home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / BNU22SR1.ZIP / src / binutils.2 / bfd / elf32.c < prev    next >
C/C++ Source or Header  |  1993-05-30  |  82KB  |  2,806 lines

  1. /* ELF executable support for BFD.
  2.    Copyright 1991, 1992, 1993 Free Software Foundation, Inc.
  3.  
  4.    Written by Fred Fish @ Cygnus Support, from information published
  5.    in "UNIX System V Release 4, Programmers Guide: ANSI C and
  6.    Programming Support Tools".  Sufficient support for gdb.
  7.  
  8.    Rewritten by Mark Eichin @ Cygnus Support, from information
  9.    published in "System V Application Binary Interface", chapters 4
  10.    and 5, as well as the various "Processor Supplement" documents
  11.    derived from it. Added support for assembler and other object file
  12.    utilities.
  13.    
  14. This file is part of BFD, the Binary File Descriptor library.
  15.  
  16. This program is free software; you can redistribute it and/or modify
  17. it under the terms of the GNU General Public License as published by
  18. the Free Software Foundation; either version 2 of the License, or
  19. (at your option) any later version.
  20.  
  21. This program is distributed in the hope that it will be useful,
  22. but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24. GNU General Public License for more details.
  25.  
  26. You should have received a copy of the GNU General Public License
  27. along with this program; if not, write to the Free Software
  28. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  29.  
  30.  
  31.     /****************************************
  32.  
  33.               WARNING
  34.  
  35.     This is only a partial ELF implementation,
  36.     incorporating only those parts that are
  37.     required to get gdb up and running.  It is
  38.     expected that it will be expanded to a full
  39.     ELF implementation at some future date.
  40.  
  41.     Unimplemented stubs call abort() to ensure
  42.     that they get proper attention if they are
  43.     ever called.  The stubs are here since
  44.     this version was hacked from the COFF
  45.     version, and thus they will probably
  46.     go away or get expanded appropriately in a
  47.     future version.
  48.  
  49.     fnf@cygnus.com
  50.  
  51.     *****************************************/
  52.  
  53.  
  54. /* Problems and other issues to resolve.
  55.  
  56.    (1)    BFD expects there to be some fixed number of "sections" in
  57.         the object file.  I.E. there is a "section_count" variable in the
  58.     bfd structure which contains the number of sections.  However, ELF
  59.     supports multiple "views" of a file.  In particular, with current
  60.     implementations, executable files typically have two tables, a
  61.     program header table and a section header table, both of which
  62.     partition the executable.
  63.  
  64.     In ELF-speak, the "linking view" of the file uses the section header
  65.     table to access "sections" within the file, and the "execution view"
  66.     uses the program header table to access "segments" within the file.
  67.     "Segments" typically may contain all the data from one or more
  68.     "sections".
  69.  
  70.     Note that the section header table is optional in ELF executables,
  71.     but it is this information that is most useful to gdb.  If the
  72.     section header table is missing, then gdb should probably try
  73.     to make do with the program header table.  (FIXME)
  74.  
  75. */
  76.  
  77. #include <string.h>        /* For strrchr and friends */
  78. #include "bfd.h"
  79. #include "sysdep.h"
  80. #include "libbfd.h"
  81. #include "libelf.h"
  82.  
  83. #ifdef HAVE_PROCFS    /* Some core file support requires host /proc files */
  84. #include <sys/procfs.h>
  85. #else
  86. #define bfd_prstatus(abfd, descdata, descsz, filepos)    /* Define away */
  87. #define bfd_fpregset(abfd, descdata, descsz, filepos)    /* Define away */
  88. #define bfd_prpsinfo(abfd, descdata, descsz, filepos)    /* Define away */
  89. #endif
  90.  
  91. /* Forward declarations of static functions */
  92.  
  93. static char *
  94. elf_read PARAMS ((bfd *, long, int));
  95.  
  96. static struct sec *
  97. section_from_elf_index PARAMS ((bfd *, int));
  98.  
  99. static int
  100. elf_section_from_bfd_section PARAMS ((bfd *, struct sec *));
  101.  
  102. static boolean
  103. elf_slurp_symbol_table PARAMS ((bfd *, asymbol **));
  104.  
  105. static char *
  106. elf_get_str_section PARAMS ((bfd *, unsigned int));
  107.      
  108. /* Some private data is stashed away for future use using the tdata pointer
  109.    in the bfd structure.  */
  110.  
  111. struct elf_obj_tdata
  112. {
  113.   Elf_Internal_Ehdr elf_header[1];    /* Actual data, but ref like ptr */
  114.   Elf_Internal_Shdr *elf_sect_ptr;
  115.   struct strtab     *strtab_ptr;
  116.   int            symtab_section;
  117.   void             *prstatus;        /* The raw /proc prstatus structure */
  118.   void             *prpsinfo;        /* The raw /proc prpsinfo structure */
  119.   Elf_External_Sym  *raw_syms;
  120.   Elf_Internal_Sym  *internal_syms;
  121.   elf_symbol_type   *symbols;
  122. };
  123.  
  124. #define elf_tdata(bfd)        ((bfd) -> tdata.elf_obj_data)
  125. #define elf_elfheader(bfd)    (elf_tdata(bfd) -> elf_header)
  126. #define elf_elfsections(bfd)    (elf_tdata(bfd) -> elf_sect_ptr)
  127. #define elf_shstrtab(bfd)    (elf_tdata(bfd) -> strtab_ptr)
  128. #define elf_onesymtab(bfd)    (elf_tdata(bfd) -> symtab_section)
  129. #define core_prpsinfo(bfd)    (elf_tdata(bfd) -> prpsinfo)
  130. #define core_prstatus(bfd)    (elf_tdata(bfd) -> prstatus)
  131. #define obj_symbols(bfd)    (elf_tdata(bfd) -> symbols)
  132. #define obj_raw_syms(bfd)    (elf_tdata(bfd) -> raw_syms)
  133. #define obj_internal_syms(bfd)    (elf_tdata(bfd) -> internal_syms)
  134.  
  135. /* Translate an ELF symbol in external format into an ELF symbol in internal
  136.    format. */
  137.  
  138. static void
  139. DEFUN(elf_swap_symbol_in,(abfd, src, dst),
  140.       bfd               *abfd AND
  141.       Elf_External_Sym *src AND
  142.       Elf_Internal_Sym *dst)
  143. {
  144.   dst -> st_name = bfd_h_get_32 (abfd, (bfd_byte *) src -> st_name);
  145.   dst -> st_value = bfd_h_get_32 (abfd, (bfd_byte *) src -> st_value);
  146.   dst -> st_size = bfd_h_get_32 (abfd, (bfd_byte *) src -> st_size);
  147.   dst -> st_info = bfd_h_get_8 (abfd, (bfd_byte *) src -> st_info);
  148.   dst -> st_other = bfd_h_get_8 (abfd, (bfd_byte *) src -> st_other);
  149.   dst -> st_shndx = bfd_h_get_16 (abfd, (bfd_byte *) src -> st_shndx);
  150. }
  151.  
  152. /* Translate an ELF symbol in internal format into an ELF symbol in external
  153.    format. */
  154.  
  155. static void
  156. DEFUN(elf_swap_symbol_out,(abfd, src, dst),
  157.       bfd               *abfd AND
  158.       Elf_Internal_Sym *src AND
  159.       Elf_External_Sym *dst)
  160. {
  161.   bfd_h_put_32 (abfd, src->st_name, dst->st_name);
  162.   bfd_h_put_32 (abfd, src->st_value, dst->st_value);
  163.   bfd_h_put_32 (abfd, src->st_size, dst->st_size);
  164.   bfd_h_put_8  (abfd, src->st_info, dst->st_info);
  165.   bfd_h_put_8  (abfd, src->st_other, dst->st_other);
  166.   bfd_h_put_16 (abfd, src->st_shndx, dst->st_shndx);
  167. }
  168.  
  169.  
  170. /* Translate an ELF file header in external format into an ELF file header in
  171.    internal format. */
  172.  
  173. static void
  174. DEFUN(elf_swap_ehdr_in,(abfd, src, dst),
  175.       bfd               *abfd AND
  176.       Elf_External_Ehdr *src AND
  177.       Elf_Internal_Ehdr *dst)
  178. {
  179.   memcpy (dst -> e_ident, src -> e_ident, EI_NIDENT);
  180.   dst -> e_type = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_type);
  181.   dst -> e_machine = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_machine);
  182.   dst -> e_version = bfd_h_get_32 (abfd, (bfd_byte *) src -> e_version);
  183.   dst -> e_entry = bfd_h_get_32 (abfd, (bfd_byte *) src -> e_entry);
  184.   dst -> e_phoff = bfd_h_get_32 (abfd, (bfd_byte *) src -> e_phoff);
  185.   dst -> e_shoff = bfd_h_get_32 (abfd, (bfd_byte *) src -> e_shoff);
  186.   dst -> e_flags = bfd_h_get_32 (abfd, (bfd_byte *) src -> e_flags);
  187.   dst -> e_ehsize = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_ehsize);
  188.   dst -> e_phentsize = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_phentsize);
  189.   dst -> e_phnum = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_phnum);
  190.   dst -> e_shentsize = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_shentsize);
  191.   dst -> e_shnum = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_shnum);
  192.   dst -> e_shstrndx = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_shstrndx);
  193. }
  194.  
  195. /* Translate an ELF file header in internal format into an ELF file header in
  196.    external format. */
  197.  
  198. static void
  199. DEFUN(elf_swap_ehdr_out,(abfd, src, dst),
  200.       bfd               *abfd AND
  201.       Elf_Internal_Ehdr *src AND
  202.       Elf_External_Ehdr *dst)
  203. {
  204.   memcpy (dst -> e_ident, src -> e_ident, EI_NIDENT);
  205.   /* note that all elements of dst are *arrays of unsigned char* already... */
  206.   bfd_h_put_16 (abfd, src->e_type, dst->e_type);
  207.   bfd_h_put_16 (abfd, src->e_machine, dst->e_machine);
  208.   bfd_h_put_32 (abfd, src->e_version, dst->e_version);
  209.   bfd_h_put_32 (abfd, src->e_entry, dst->e_entry);
  210.   bfd_h_put_32 (abfd, src->e_phoff, dst->e_phoff);
  211.   bfd_h_put_32 (abfd, src->e_shoff, dst->e_shoff);
  212.   bfd_h_put_32 (abfd, src->e_flags, dst->e_flags);
  213.   bfd_h_put_16 (abfd, src->e_ehsize, dst->e_ehsize);
  214.   bfd_h_put_16 (abfd, src->e_phentsize, dst->e_phentsize);
  215.   bfd_h_put_16 (abfd, src->e_phnum, dst->e_phnum);
  216.   bfd_h_put_16 (abfd, src->e_shentsize, dst->e_shentsize);
  217.   bfd_h_put_16 (abfd, src->e_shnum, dst->e_shnum);
  218.   bfd_h_put_16 (abfd, src->e_shstrndx, dst->e_shstrndx);
  219. }
  220.  
  221.  
  222. /* Translate an ELF section header table entry in external format into an
  223.    ELF section header table entry in internal format. */
  224.  
  225. static void
  226. DEFUN(elf_swap_shdr_in,(abfd, src, dst),
  227.       bfd               *abfd AND
  228.       Elf_External_Shdr *src AND
  229.       Elf_Internal_Shdr *dst)
  230. {
  231.   dst->sh_name = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_name);
  232.   dst->sh_type = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_type);
  233.   dst->sh_flags = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_flags);
  234.   dst->sh_addr = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_addr);
  235.   dst->sh_offset = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_offset);
  236.   dst->sh_size = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_size);
  237.   dst->sh_link = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_link);
  238.   dst->sh_info = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_info);
  239.   dst->sh_addralign = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_addralign);
  240.   dst->sh_entsize = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_entsize);
  241.   /* we haven't done any processing on it yet, so... */
  242.   dst->rawdata = (void*)0;
  243. }
  244.  
  245. /* Translate an ELF section header table entry in internal format into an
  246.    ELF section header table entry in external format. */
  247.  
  248. static void
  249. DEFUN(elf_swap_shdr_out,(abfd, src, dst),
  250.       bfd               *abfd AND
  251.       Elf_Internal_Shdr *src AND
  252.       Elf_External_Shdr *dst)
  253. {
  254.   /* note that all elements of dst are *arrays of unsigned char* already... */
  255.   bfd_h_put_32 (abfd, src->sh_name, dst->sh_name);
  256.   bfd_h_put_32 (abfd, src->sh_type, dst->sh_type);
  257.   bfd_h_put_32 (abfd, src->sh_flags, dst->sh_flags);
  258.   bfd_h_put_32 (abfd, src->sh_addr, dst->sh_addr);
  259.   bfd_h_put_32 (abfd, src->sh_offset, dst->sh_offset);
  260.   bfd_h_put_32 (abfd, src->sh_size, dst->sh_size);
  261.   bfd_h_put_32 (abfd, src->sh_link, dst->sh_link);
  262.   bfd_h_put_32 (abfd, src->sh_info, dst->sh_info);
  263.   bfd_h_put_32 (abfd, src->sh_addralign, dst->sh_addralign);
  264.   bfd_h_put_32 (abfd, src->sh_entsize, dst->sh_entsize);
  265. }
  266.  
  267.  
  268. /* Translate an ELF program header table entry in external format into an
  269.    ELF program header table entry in internal format. */
  270.  
  271. static void
  272. DEFUN(elf_swap_phdr_in,(abfd, src, dst),
  273.       bfd               *abfd AND
  274.       Elf_External_Phdr *src AND
  275.       Elf_Internal_Phdr *dst)
  276. {
  277.   dst->p_type = bfd_h_get_32 (abfd, (bfd_byte *) src->p_type);
  278.   dst->p_offset = bfd_h_get_32 (abfd, (bfd_byte *) src->p_offset);
  279.   dst->p_vaddr = bfd_h_get_32 (abfd, (bfd_byte *) src->p_vaddr);
  280.   dst->p_paddr = bfd_h_get_32 (abfd, (bfd_byte *) src->p_paddr);
  281.   dst->p_filesz = bfd_h_get_32 (abfd, (bfd_byte *) src->p_filesz);
  282.   dst->p_memsz = bfd_h_get_32 (abfd, (bfd_byte *) src->p_memsz);
  283.   dst->p_flags = bfd_h_get_32 (abfd, (bfd_byte *) src->p_flags);
  284.   dst->p_align = bfd_h_get_32 (abfd, (bfd_byte *) src->p_align);
  285. }
  286.  
  287. /* ... */
  288.  
  289. static void
  290. DEFUN(elf_swap_phdr_out,(abfd, src, dst),
  291.       bfd               *abfd AND
  292.       Elf_Internal_Phdr *src AND
  293.       Elf_External_Phdr *dst)
  294. {
  295.   /* note that all elements of dst are *arrays of unsigned char* already... */
  296.   bfd_h_put_32 (abfd, src->p_type, dst->p_type);
  297.   bfd_h_put_32 (abfd, src->p_offset, dst->p_offset);
  298.   bfd_h_put_32 (abfd, src->p_vaddr, dst->p_vaddr);
  299.   bfd_h_put_32 (abfd, src->p_paddr, dst->p_paddr);
  300.   bfd_h_put_32 (abfd, src->p_filesz, dst->p_filesz);
  301.   bfd_h_put_32 (abfd, src->p_memsz, dst->p_memsz);
  302.   bfd_h_put_32 (abfd, src->p_flags, dst->p_flags);
  303.   bfd_h_put_32 (abfd, src->p_align, dst->p_align);
  304. }
  305.  
  306. /* Translate an ELF reloc from external format to internal format. */
  307. static void
  308. DEFUN(elf_swap_reloc_in,(abfd, src, dst),
  309.       bfd            *abfd AND
  310.       Elf_External_Rel *src AND
  311.       Elf_Internal_Rel *dst)
  312. {
  313.   dst->r_offset = bfd_h_get_32 (abfd, (bfd_byte *) src->r_offset);
  314.   dst->r_info = bfd_h_get_32 (abfd, (bfd_byte *) src->r_info);
  315. }
  316.  
  317. static void
  318. DEFUN(elf_swap_reloca_in,(abfd, src, dst),
  319.       bfd            *abfd AND
  320.       Elf_External_Rela *src AND
  321.       Elf_Internal_Rela *dst)
  322. {
  323.   dst->r_offset = bfd_h_get_32 (abfd, (bfd_byte *) src->r_offset);
  324.   dst->r_info = bfd_h_get_32 (abfd, (bfd_byte *) src->r_info);
  325.   dst->r_addend = bfd_h_get_32 (abfd, (bfd_byte *) src->r_addend);
  326. }
  327.  
  328. /* Translate an ELF reloc from internal format to external format. */
  329. static void
  330. DEFUN(elf_swap_reloc_out,(abfd, src, dst),
  331.       bfd            *abfd AND
  332.       Elf_Internal_Rel *src AND
  333.       Elf_External_Rel *dst)
  334. {
  335.   bfd_h_put_32 (abfd, src->r_offset, dst->r_offset);
  336.   bfd_h_put_32 (abfd, src->r_info, dst->r_info);
  337. }
  338.  
  339. static void
  340. DEFUN(elf_swap_reloca_out,(abfd, src, dst),
  341.       bfd            *abfd AND
  342.       Elf_Internal_Rela *src AND
  343.       Elf_External_Rela *dst)
  344. {
  345.   bfd_h_put_32 (abfd, src->r_offset, dst->r_offset);
  346.   bfd_h_put_32 (abfd, src->r_info, dst->r_info);
  347.   bfd_h_put_32 (abfd, src->r_addend, dst->r_addend);
  348. }
  349.  
  350. /* 
  351. INTERNAL_FUNCTION
  352.     bfd_elf_find_section
  353.  
  354. SYNOPSIS
  355.     struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
  356.  
  357. DESCRIPTION
  358.     Helper functions for GDB to locate the string tables.
  359.     Since BFD hides string tables from callers, GDB needs to use an
  360.     internal hook to find them.  Sun's .stabstr, in particular,
  361.     isn't even pointed to by the .stab section, so ordinary
  362.     mechanisms wouldn't work to find it, even if we had some.
  363. */
  364.  
  365. struct elf_internal_shdr *
  366. DEFUN(bfd_elf_find_section, (abfd, name),
  367.       bfd        *abfd AND
  368.       char        *name)
  369. {
  370.   Elf_Internal_Shdr *i_shdrp;
  371.   Elf_Internal_Shdr *gotit = NULL;
  372.   char *shstrtab;
  373.   unsigned int max;
  374.   unsigned int i;
  375.  
  376.   i_shdrp = elf_elfsections (abfd);
  377.   if (i_shdrp != NULL)
  378.     {
  379.       shstrtab = elf_get_str_section (abfd, elf_elfheader (abfd)->e_shstrndx);
  380.       if (shstrtab != NULL)
  381.     {
  382.       max = elf_elfheader (abfd)->e_shnum;
  383.       for (i = 1; i < max; i++)
  384.         {
  385.           if (!strcmp (&shstrtab[i_shdrp[i].sh_name], name))
  386.         {
  387.           gotit = &i_shdrp[i];
  388.         }
  389.         }
  390.     }
  391.     }
  392.   return (gotit);
  393. }
  394.  
  395. /* End of GDB support.  */
  396.  
  397. static char *
  398. DEFUN(elf_get_str_section, (abfd, shindex),
  399.       bfd        *abfd AND
  400.       unsigned int    shindex)
  401. {
  402.   Elf_Internal_Shdr *i_shdrp;
  403.   char *shstrtab = NULL;
  404.   unsigned int offset;
  405.   unsigned int shstrtabsize;
  406.  
  407.   i_shdrp = elf_elfsections (abfd);
  408.   if (i_shdrp != NULL)
  409.     {
  410.       shstrtab = i_shdrp[shindex].rawdata;
  411.       if (shstrtab == NULL)
  412.     {
  413.       /* No cached one, attempt to read, and cache what we read. */
  414.       offset = i_shdrp[shindex].sh_offset;
  415.       shstrtabsize = i_shdrp[shindex].sh_size;
  416.       shstrtab = elf_read (abfd, offset, shstrtabsize);
  417.       i_shdrp[shindex].rawdata = (void*) shstrtab;
  418.     }
  419.     }
  420.   return (shstrtab);
  421. }
  422.  
  423. static char *
  424. DEFUN(elf_string_from_elf_section, (abfd, shindex, strindex),
  425.       bfd        *abfd AND
  426.       unsigned int    shindex AND
  427.       unsigned int    strindex)
  428. {
  429.   Elf_Internal_Shdr *i_shdrp = elf_elfsections (abfd);
  430.   Elf_Internal_Shdr *hdr = i_shdrp + shindex;
  431.  
  432.   if (! hdr->rawdata)
  433.     {
  434.       if (elf_get_str_section (abfd, shindex) == NULL)
  435.     {
  436.       return NULL;
  437.     }
  438.     }
  439.   return ((char*)hdr->rawdata)+strindex;
  440. }
  441.  
  442. #define elf_string_from_elf_strtab(abfd, strindex) \
  443.   elf_string_from_elf_section (abfd, elf_elfheader(abfd)->e_shstrndx, strindex)
  444.  
  445. /* Create a new bfd section from an ELF section header. */
  446.  
  447. static boolean
  448. DEFUN(bfd_section_from_shdr, (abfd, shindex),
  449.       bfd               *abfd AND
  450.       unsigned int    shindex)
  451. {
  452.   Elf_Internal_Shdr *i_shdrp = elf_elfsections (abfd);
  453.   Elf_Internal_Shdr *hdr = i_shdrp + shindex;
  454.   asection *newsect;
  455.   char *name;
  456.  
  457.   name = hdr->sh_name ? elf_string_from_elf_strtab (abfd, hdr->sh_name) : "";
  458.  
  459.   switch(hdr->sh_type) {
  460.  
  461.   case SHT_NULL:
  462.     /* inactive section. Throw it away. */
  463.     return true;
  464.  
  465.   case SHT_PROGBITS:
  466.     /* Bits that get saved. This one is real. */
  467.     if (! hdr->rawdata ) 
  468.       {
  469.     newsect = bfd_make_section (abfd, name);
  470.     if (newsect != NULL)
  471.       {
  472.         newsect->vma = hdr->sh_addr;
  473.         newsect->_raw_size = hdr->sh_size;
  474.         newsect->filepos = hdr->sh_offset; /* so we can read back the bits */
  475.         newsect->flags |= SEC_HAS_CONTENTS;
  476.         
  477.         if (hdr->sh_flags & SHF_ALLOC)
  478.           {
  479.         newsect->flags |= SEC_ALLOC;
  480.         newsect->flags |= SEC_LOAD;
  481.           }
  482.         
  483.         if (!(hdr->sh_flags & SHF_WRITE))
  484.           newsect->flags |= SEC_READONLY;
  485.         
  486.         if (hdr->sh_flags & SHF_EXECINSTR)
  487.           newsect->flags |= SEC_CODE;    /* FIXME: may only contain SOME code */
  488.         else
  489.           newsect->flags |= SEC_DATA;
  490.         
  491.         hdr->rawdata = (void*)newsect;
  492.       }
  493.       }
  494.     return true;
  495.  
  496.   case SHT_NOBITS:
  497.     /* Bits that get saved. This one is real. */
  498.     if (! hdr->rawdata ) 
  499.       {
  500.     newsect = bfd_make_section (abfd, name);
  501.     if (newsect != NULL)
  502.       {
  503.         newsect->vma = hdr->sh_addr;
  504.         newsect->_raw_size = hdr->sh_size;
  505.         newsect->filepos = hdr->sh_offset; /* fake */
  506.         if (hdr->sh_flags & SHF_ALLOC)
  507.           newsect->flags |= SEC_ALLOC;
  508.  
  509.         if (!(hdr->sh_flags & SHF_WRITE))
  510.           newsect->flags |= SEC_READONLY;
  511.  
  512.         if (hdr->sh_flags & SHF_EXECINSTR)
  513.           newsect->flags |= SEC_CODE;    /* FIXME: may only contain SOME code */
  514.         else
  515.           newsect->flags |= SEC_DATA;
  516.  
  517.         hdr->rawdata = (void*)newsect;
  518.       }
  519.       }
  520.     return true;
  521.  
  522.   case SHT_SYMTAB:            /* A symbol table */
  523.     BFD_ASSERT (hdr->sh_entsize == sizeof (Elf_External_Sym));
  524.     elf_onesymtab (abfd) = shindex;
  525.     abfd->flags |= HAS_SYMS;
  526.     return true;
  527.  
  528.   case SHT_STRTAB:            /* A string table */
  529.     return true;
  530.  
  531.   case SHT_REL:
  532.   case SHT_RELA:
  533.     /* *these* do a lot of work -- but build no sections! */
  534.     /* the spec says there can be multiple strtabs, but only one symtab */
  535.     /* but there can be lots of REL* sections. */
  536.     /* FIXME:  The above statement is wrong!  There are typically at least
  537.        two symbol tables in a dynamically linked executable, ".dynsym"
  538.        which is the dynamic linkage symbol table and ".symtab", which is
  539.        the "traditional" symbol table.  -fnf */
  540.        
  541.     {
  542.       asection        *target_sect;
  543.       
  544.       bfd_section_from_shdr (abfd, hdr->sh_link); /* symbol table */
  545.       bfd_section_from_shdr (abfd, hdr->sh_info); /* target */
  546.       target_sect = section_from_elf_index (abfd, hdr->sh_info);
  547.       if (target_sect == NULL)
  548.       return false;
  549.  
  550. #if 0
  551.       /* FIXME:  We are only prepared to read one symbol table, so
  552.      do NOT read the dynamic symbol table since it is only a
  553.      subset of the full symbol table.  Also see comment above. -fnf */
  554.       if (!elf_slurp_symbol_table(abfd, i_shdrp + hdr->sh_link))
  555.     return false;
  556. #endif
  557.  
  558.       target_sect->reloc_count = hdr->sh_size / hdr->sh_entsize;
  559.       target_sect->flags |= SEC_RELOC;
  560.       target_sect->relocation = 0;
  561.       target_sect->rel_filepos = hdr->sh_offset;
  562.       return true;
  563.     }
  564.     break;
  565.  
  566.   case SHT_HASH:
  567.   case SHT_DYNAMIC:
  568.   case SHT_DYNSYM:        /* could treat this like symtab... */
  569. #if 0
  570.     fprintf(stderr, "Dynamic Linking sections not yet supported.\n");
  571.     abort ();
  572. #endif
  573.     break;
  574.  
  575.   case SHT_NOTE:
  576. #if 0
  577.     fprintf(stderr, "Note Sections not yet supported.\n");
  578.     abort ();
  579. #endif
  580.     break;
  581.  
  582.   case SHT_SHLIB:
  583. #if 0
  584.     fprintf(stderr, "SHLIB Sections not supported (and non conforming.)\n");
  585. #endif
  586.     return true;
  587.  
  588.   default:
  589.     break;
  590.   }
  591.   
  592.   return (true);
  593. }
  594.  
  595.  
  596.  
  597.  
  598. struct strtab {
  599.   char *tab;
  600.   int nentries;
  601.   int length;
  602. };
  603.  
  604.  
  605. static struct strtab *
  606. DEFUN(bfd_new_strtab, (abfd),
  607.       bfd        *abfd)
  608. {
  609.   struct strtab *ss;
  610.  
  611.   ss = (struct strtab *) bfd_xmalloc(sizeof(struct strtab));
  612.   ss->tab = bfd_xmalloc(1);
  613.   BFD_ASSERT(ss->tab != 0);
  614.   *ss->tab = 0;
  615.   ss->nentries = 0;
  616.   ss->length = 1;
  617.  
  618.   return ss;
  619. }
  620.  
  621. static int
  622. DEFUN(bfd_add_to_strtab, (abfd, ss, str),
  623.       bfd        *abfd AND
  624.       struct strtab    *ss AND
  625.       CONST char    *str)
  626. {
  627.   /* should search first, but for now: */
  628.   /* include the trailing NUL */
  629.   int ln = strlen(str)+1;
  630.   
  631.   /* should this be using obstacks? */
  632.   ss->tab = realloc(ss->tab, ss->length + ln);
  633.  
  634.   BFD_ASSERT(ss->tab != 0);
  635.   strcpy(ss->tab + ss->length, str);
  636.   ss->nentries++;
  637.   ss->length += ln;
  638.  
  639.   return ss->length - ln;
  640. }
  641.  
  642. static int
  643. DEFUN(bfd_add_2_to_strtab, (abfd, ss, str, str2),
  644.       bfd        *abfd AND
  645.       struct strtab    *ss AND
  646.       char        *str AND
  647.       CONST char    *str2)
  648. {
  649.   /* should search first, but for now: */
  650.   /* include the trailing NUL */
  651.   int ln = strlen(str)+strlen(str2)+1;
  652.   
  653.   /* should this be using obstacks? */
  654.   if (ss->length)
  655.     ss->tab = realloc(ss->tab, ss->length + ln);
  656.   else 
  657.     ss->tab = bfd_xmalloc(ln);
  658.  
  659.   BFD_ASSERT(ss->tab != 0);
  660.   strcpy(ss->tab + ss->length, str);
  661.   strcpy(ss->tab + ss->length + strlen(str), str2);
  662.   ss->nentries++;
  663.   ss->length += ln;
  664.  
  665.   return ss->length - ln;
  666. }
  667.  
  668. /* Create a new ELF section from a bfd section. */
  669.  
  670. static boolean
  671. DEFUN(bfd_shdr_from_section, (abfd, hdr, shstrtab, indx),
  672.       bfd               *abfd AND
  673.       Elf_Internal_Shdr *hdr AND
  674.       struct strtab    *shstrtab AND
  675.       int        indx)
  676. {
  677.   asection *sect;
  678.   int ndx;
  679.   
  680.   /* figure out out to write the section name from the bfd section name. MWE */
  681.             
  682.   sect = abfd->sections;
  683.   for (ndx = indx; --ndx; )
  684.     {
  685.       sect = sect->next;
  686.     }
  687.   hdr[indx].sh_name = bfd_add_to_strtab(abfd, shstrtab,
  688.                     bfd_section_name(abfd, sect));
  689.   hdr[indx].sh_addr = sect->vma;
  690.   hdr[indx].sh_size = sect->_raw_size;
  691.   hdr[indx].sh_flags = 0;
  692.   /* these need to be preserved on */
  693.   hdr[indx].sh_link = 0;
  694.   hdr[indx].sh_info = 0;
  695.   hdr[indx].sh_addralign = 0;
  696.   hdr[indx].sh_entsize = 0;
  697.  
  698.   hdr[indx].sh_type = 0;
  699.   if (sect->flags & SEC_RELOC) {
  700.     hdr[indx].sh_type = SHT_RELA; /* FIXME -- sparc specific */
  701.   }
  702.   
  703.   if (sect->flags & SEC_HAS_CONTENTS)
  704.     {
  705.       hdr[indx].sh_offset = sect->filepos;
  706.       hdr[indx].sh_size = sect->_raw_size;
  707.     }
  708.   if (sect->flags & SEC_ALLOC)
  709.     {
  710.       hdr[indx].sh_flags |= SHF_ALLOC;
  711.       if (sect->flags & SEC_LOAD)
  712.     {
  713.       /* do something with sh_type ? */
  714.     }
  715.     }
  716.   if (!(sect->flags & SEC_READONLY)) 
  717.     hdr[indx].sh_flags |= SHF_WRITE;
  718.  
  719.   if (sect->flags & SEC_CODE)
  720.     hdr[indx].sh_flags |= SHF_EXECINSTR;
  721.  
  722.   return (true);
  723. }
  724.  
  725. /* Create a new bfd section from an ELF program header.
  726.  
  727.    Since program segments have no names, we generate a synthetic name
  728.    of the form segment<NUM>, where NUM is generally the index in the
  729.    program header table.  For segments that are split (see below) we
  730.    generate the names segment<NUM>a and segment<NUM>b.
  731.  
  732.    Note that some program segments may have a file size that is different than
  733.    (less than) the memory size.  All this means is that at execution the
  734.    system must allocate the amount of memory specified by the memory size,
  735.    but only initialize it with the first "file size" bytes read from the
  736.    file.  This would occur for example, with program segments consisting
  737.    of combined data+bss.
  738.  
  739.    To handle the above situation, this routine generates TWO bfd sections
  740.    for the single program segment.  The first has the length specified by
  741.    the file size of the segment, and the second has the length specified
  742.    by the difference between the two sizes.  In effect, the segment is split
  743.    into it's initialized and uninitialized parts.
  744.  
  745.  */
  746.  
  747. static boolean
  748. DEFUN(bfd_section_from_phdr, (abfd, hdr, index),
  749.       bfd               *abfd AND
  750.       Elf_Internal_Phdr *hdr AND
  751.       int         index)
  752. {
  753.   asection *newsect;
  754.   char *name;
  755.   char namebuf[64];
  756.   int split;
  757.  
  758.   split = ((hdr -> p_memsz > 0) &&
  759.        (hdr -> p_filesz > 0) &&
  760.        (hdr -> p_memsz > hdr -> p_filesz));
  761.   sprintf (namebuf, split ? "segment%da" : "segment%d", index);
  762.   name = bfd_alloc (abfd, strlen (namebuf) + 1);
  763.   strcpy (name, namebuf);
  764.   newsect = bfd_make_section (abfd, name);
  765.   newsect -> vma = hdr -> p_vaddr;
  766.   newsect -> _raw_size = hdr -> p_filesz;
  767.   newsect -> filepos = hdr -> p_offset;
  768.   newsect -> flags |= SEC_HAS_CONTENTS;
  769.   if (hdr -> p_type == PT_LOAD)
  770.     {
  771.       newsect -> flags |= SEC_ALLOC;
  772.       newsect -> flags |= SEC_LOAD;
  773.       if (hdr -> p_flags & PF_X)
  774.     {
  775.       /* FIXME: all we known is that it has execute PERMISSION,
  776.          may be data. */
  777.       newsect -> flags |= SEC_CODE;
  778.     }
  779.     }
  780.   if (!(hdr -> p_flags & PF_W))
  781.     {
  782.       newsect -> flags |= SEC_READONLY;
  783.     }
  784.  
  785.   if (split)
  786.     {
  787.       sprintf (namebuf, "segment%db", index);
  788.       name = bfd_alloc (abfd, strlen (namebuf) + 1);
  789.       strcpy (name, namebuf);
  790.       newsect = bfd_make_section (abfd, name);
  791.       newsect -> vma = hdr -> p_vaddr + hdr -> p_filesz;
  792.       newsect -> _raw_size = hdr -> p_memsz - hdr -> p_filesz;
  793.       if (hdr -> p_type == PT_LOAD)
  794.     {
  795.       newsect -> flags |= SEC_ALLOC;
  796.       if (hdr -> p_flags & PF_X)
  797.         newsect -> flags |= SEC_CODE;
  798.     }
  799.       if (!(hdr -> p_flags & PF_W))
  800.     newsect -> flags |= SEC_READONLY;
  801.     }
  802.  
  803.   return (true);
  804. }
  805.  
  806. #ifdef HAVE_PROCFS
  807.  
  808. static void
  809. DEFUN(bfd_prstatus,(abfd, descdata, descsz, filepos),
  810.       bfd    *abfd AND
  811.       char    *descdata AND
  812.       int     descsz AND
  813.       long     filepos)
  814. {
  815.   asection *newsect;
  816.   prstatus_t *status = (prstatus_t *)0;
  817.  
  818.   if (descsz == sizeof (prstatus_t))
  819.     {
  820.       newsect = bfd_make_section (abfd, ".reg");
  821.       newsect -> _raw_size = sizeof (status->pr_reg);
  822.       newsect -> filepos = filepos + (long) &status->pr_reg;
  823.       newsect -> flags = SEC_ALLOC | SEC_HAS_CONTENTS;
  824.       newsect -> alignment_power = 2;
  825.       if ((core_prstatus (abfd) = bfd_alloc (abfd, descsz)) != NULL)
  826.     {
  827.       memcpy (core_prstatus (abfd), descdata, descsz);
  828.     }
  829.     }
  830. }
  831.  
  832. /* Stash a copy of the prpsinfo structure away for future use. */
  833.  
  834. static void
  835. DEFUN(bfd_prpsinfo,(abfd, descdata, descsz, filepos),
  836.       bfd    *abfd AND
  837.       char    *descdata AND
  838.       int     descsz AND
  839.       long     filepos)
  840. {
  841.   asection *newsect;
  842.  
  843.   if (descsz == sizeof (prpsinfo_t))
  844.     {
  845.       if ((core_prpsinfo (abfd) = bfd_alloc (abfd, descsz)) != NULL)
  846.     {
  847.       memcpy (core_prpsinfo (abfd), descdata, descsz);
  848.     }
  849.     }
  850. }
  851.  
  852. static void
  853. DEFUN(bfd_fpregset,(abfd, descdata, descsz, filepos),
  854.       bfd    *abfd AND
  855.       char    *descdata AND
  856.       int     descsz AND
  857.       long     filepos)
  858. {
  859.   asection *newsect;
  860.  
  861.   newsect = bfd_make_section (abfd, ".reg2");
  862.   newsect -> _raw_size = descsz;
  863.   newsect -> filepos = filepos;
  864.   newsect -> flags = SEC_ALLOC | SEC_HAS_CONTENTS;
  865.   newsect -> alignment_power = 2;
  866. }
  867.  
  868. #endif    /* HAVE_PROCFS */
  869.  
  870. /* Return a pointer to the args (including the command name) that were
  871.    seen by the program that generated the core dump.  Note that for
  872.    some reason, a spurious space is tacked onto the end of the args
  873.    in some (at least one anyway) implementations, so strip it off if
  874.    it exists. */
  875.  
  876. char *
  877. DEFUN(elf_core_file_failing_command, (abfd),
  878.      bfd *abfd)
  879. {
  880. #ifdef HAVE_PROCFS
  881.   if (core_prpsinfo (abfd))
  882.     {
  883.       prpsinfo_t *p = core_prpsinfo (abfd);
  884.       char *scan = p -> pr_psargs;
  885.       while (*scan++) {;}
  886.       scan -= 2;
  887.       if ((scan > p -> pr_psargs) && (*scan == ' '))
  888.     {
  889.       *scan = '\000';
  890.     }
  891.       return (p -> pr_psargs);
  892.     }
  893. #endif
  894.   return (NULL);
  895. }
  896.  
  897. /* Return the number of the signal that caused the core dump.  Presumably,
  898.    since we have a core file, we got a signal of some kind, so don't bother
  899.    checking the other process status fields, just return the signal number.
  900.    */
  901.  
  902. int
  903. DEFUN(elf_core_file_failing_signal, (abfd),
  904.       bfd *abfd)
  905. {
  906. #ifdef HAVE_PROCFS
  907.   if (core_prstatus (abfd))
  908.     {
  909.       return (((prstatus_t *)(core_prstatus (abfd))) -> pr_cursig);
  910.     }
  911. #endif
  912.   return (-1);
  913. }
  914.  
  915. /* Check to see if the core file could reasonably be expected to have
  916.    come for the current executable file.  Note that by default we return
  917.    true unless we find something that indicates that there might be a
  918.    problem.
  919.    */
  920.  
  921. boolean
  922. DEFUN(elf_core_file_matches_executable_p, (core_bfd, exec_bfd),
  923.       bfd *core_bfd AND
  924.       bfd *exec_bfd)
  925. {
  926. #ifdef HAVE_PROCFS
  927.   char *corename;
  928.   char *execname;
  929. #endif
  930.  
  931.   /* First, xvecs must match since both are ELF files for the same target. */
  932.  
  933.   if (core_bfd->xvec != exec_bfd->xvec)
  934.     {
  935.       bfd_error = system_call_error;
  936.       return (false);
  937.     }
  938.  
  939. #ifdef HAVE_PROCFS
  940.  
  941.   /* If no prpsinfo, just return true.  Otherwise, grab the last component
  942.      of the exec'd pathname from the prpsinfo. */
  943.  
  944.   if (core_prpsinfo (core_bfd))
  945.     {
  946.       corename = (((struct prpsinfo *) core_prpsinfo (core_bfd)) -> pr_fname);
  947.     }  
  948.   else
  949.     {
  950.       return (true);
  951.     }
  952.  
  953.   /* Find the last component of the executable pathname. */
  954.  
  955.   if ((execname = strrchr (exec_bfd -> filename, '/')) != NULL)
  956.     {
  957.       execname++;
  958.     }
  959.   else
  960.     {
  961.       execname = (char *) exec_bfd -> filename;
  962.     }
  963.  
  964.   /* See if they match */
  965.  
  966.   return (strcmp (execname, corename) ? false : true);
  967.  
  968. #else
  969.  
  970.   return (true);
  971.  
  972. #endif    /* HAVE_PROCFS */
  973. }
  974.  
  975. /* ELF core files contain a segment of type PT_NOTE, that holds much of
  976.    the information that would normally be available from the /proc interface
  977.    for the process, at the time the process dumped core.  Currently this
  978.    includes copies of the prstatus, prpsinfo, and fpregset structures.
  979.  
  980.    Since these structures are potentially machine dependent in size and
  981.    ordering, bfd provides two levels of support for them.  The first level,
  982.    available on all machines since it does not require that the host
  983.    have /proc support or the relevant include files, is to create a bfd
  984.    section for each of the prstatus, prpsinfo, and fpregset structures,
  985.    without any interpretation of their contents.  With just this support,
  986.    the bfd client will have to interpret the structures itself.  Even with
  987.    /proc support, it might want these full structures for it's own reasons.
  988.  
  989.    In the second level of support, where HAVE_PROCFS is defined, bfd will
  990.    pick apart the structures to gather some additional information that
  991.    clients may want, such as the general register set, the name of the
  992.    exec'ed file and its arguments, the signal (if any) that caused the
  993.    core dump, etc.
  994.  
  995.    */
  996.  
  997. static boolean
  998. DEFUN(elf_corefile_note, (abfd, hdr),
  999.       bfd               *abfd AND
  1000.       Elf_Internal_Phdr *hdr)
  1001. {
  1002.   Elf_External_Note *x_note_p;    /* Elf note, external form */
  1003.   Elf_Internal_Note i_note;    /* Elf note, internal form */
  1004.   char *buf = NULL;        /* Entire note segment contents */
  1005.   char *namedata;        /* Name portion of the note */
  1006.   char *descdata;        /* Descriptor portion of the note */
  1007.   char *sectname;        /* Name to use for new section */
  1008.   long filepos;            /* File offset to descriptor data */
  1009.   asection *newsect;
  1010.  
  1011.   if (hdr -> p_filesz > 0
  1012.       && (buf = (char *) bfd_xmalloc (hdr -> p_filesz)) != NULL
  1013.       && bfd_seek (abfd, hdr -> p_offset, SEEK_SET) != -1
  1014.       && bfd_read ((PTR) buf, hdr -> p_filesz, 1, abfd) == hdr -> p_filesz)
  1015.     {
  1016.       x_note_p = (Elf_External_Note *) buf;
  1017.       while ((char *) x_note_p < (buf + hdr -> p_filesz))
  1018.     {
  1019.       i_note.namesz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p -> namesz);
  1020.       i_note.descsz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p -> descsz);
  1021.       i_note.type = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p -> type);
  1022.       namedata = x_note_p -> name;
  1023.       descdata = namedata + BFD_ALIGN (i_note.namesz, 4);
  1024.       filepos = hdr -> p_offset + (descdata - buf);
  1025.       switch (i_note.type) {
  1026.         case NT_PRSTATUS:
  1027.           /* process descdata as prstatus info */
  1028.           bfd_prstatus (abfd, descdata, i_note.descsz, filepos);
  1029.           sectname = ".prstatus";
  1030.           break;
  1031.         case NT_FPREGSET:
  1032.           /* process descdata as fpregset info */
  1033.           bfd_fpregset (abfd, descdata, i_note.descsz, filepos);
  1034.           sectname = ".fpregset";
  1035.           break;
  1036.         case NT_PRPSINFO:
  1037.           /* process descdata as prpsinfo */
  1038.           bfd_prpsinfo (abfd, descdata, i_note.descsz, filepos);
  1039.           sectname = ".prpsinfo";
  1040.           break;
  1041.         default:
  1042.           /* Unknown descriptor, just ignore it. */
  1043.           sectname = NULL;
  1044.           break;
  1045.       }
  1046.       if (sectname != NULL)
  1047.         {
  1048.           newsect = bfd_make_section (abfd, sectname);
  1049.           newsect -> _raw_size = i_note.descsz;
  1050.           newsect -> filepos = filepos;
  1051.           newsect -> flags = SEC_ALLOC | SEC_HAS_CONTENTS;
  1052.           newsect -> alignment_power = 2;
  1053.         }
  1054.       x_note_p = (Elf_External_Note *)
  1055.             (descdata + BFD_ALIGN (i_note.descsz, 4));
  1056.     }
  1057.     }
  1058.   if (buf != NULL)
  1059.     {
  1060.       free (buf);
  1061.     }
  1062.   return true;
  1063.   
  1064. }
  1065.  
  1066.  
  1067. /* Read a specified number of bytes at a specified offset in an ELF
  1068.    file, into a newly allocated buffer, and return a pointer to the 
  1069.    buffer. */
  1070.  
  1071. static char *
  1072. DEFUN(elf_read, (abfd, offset, size),
  1073.       bfd    *abfd AND
  1074.       long    offset AND
  1075.       int    size)
  1076. {
  1077.   char *buf;
  1078.  
  1079.   if ((buf = bfd_alloc (abfd, size)) == NULL)
  1080.     {
  1081.       bfd_error = no_memory;
  1082.       return (NULL);
  1083.     }
  1084.   if (bfd_seek (abfd, offset, SEEK_SET) == -1)
  1085.     {
  1086.       bfd_error = system_call_error;
  1087.       return (NULL);
  1088.     }
  1089.   if (bfd_read ((PTR) buf, size, 1, abfd) != size)
  1090.     {
  1091.       bfd_error = system_call_error;
  1092.       return (NULL);
  1093.     }
  1094.   return (buf);
  1095. }
  1096.  
  1097. /* Begin processing a given object.
  1098.  
  1099.    First we validate the file by reading in the ELF header and checking
  1100.    the magic number.
  1101.  
  1102.    */
  1103.  
  1104. static boolean
  1105. DEFUN (elf_file_p, (x_ehdrp), Elf_External_Ehdr *x_ehdrp)
  1106. {
  1107.   return ((x_ehdrp->e_ident[EI_MAG0] == ELFMAG0)
  1108.       && (x_ehdrp->e_ident[EI_MAG1] == ELFMAG1)
  1109.       && (x_ehdrp->e_ident[EI_MAG2] == ELFMAG2)
  1110.       && (x_ehdrp->e_ident[EI_MAG3] == ELFMAG3));
  1111. }
  1112.  
  1113. bfd_target *
  1114. DEFUN (elf_object_p, (abfd), bfd *abfd)
  1115. {
  1116.   Elf_External_Ehdr x_ehdr;    /* Elf file header, external form */
  1117.   Elf_Internal_Ehdr *i_ehdrp;    /* Elf file header, internal form */
  1118.   Elf_External_Shdr x_shdr;    /* Section header table entry, external form */
  1119.   Elf_Internal_Shdr *i_shdrp;    /* Section header table, internal form */
  1120.   int shindex;
  1121.   char *shstrtab;        /* Internal copy of section header stringtab */
  1122.   struct elf_backend_data *ebd;    /* Use to get ELF_ARCH stored in xvec */
  1123.   
  1124.   /* Read in the ELF header in external format.  */
  1125.  
  1126.   if (bfd_read ((PTR) &x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr))
  1127.     {
  1128.       bfd_error = system_call_error;
  1129.       return (NULL);
  1130.     }
  1131.  
  1132.   /* Now check to see if we have a valid ELF file, and one that BFD can
  1133.      make use of.  The magic number must match, the address size ('class')
  1134.      and byte-swapping must match our XVEC entry, and it must have a
  1135.      section header table (FIXME: See comments re sections at top of this
  1136.      file). */
  1137.  
  1138.   if (elf_file_p (&x_ehdr) == false)
  1139.     {
  1140.     wrong:
  1141.       bfd_error = wrong_format;
  1142.       return (NULL);
  1143.     }
  1144.  
  1145.   /* FIXME, Check EI_VERSION here !  */
  1146.  
  1147.   switch (x_ehdr.e_ident[EI_CLASS])
  1148.     {
  1149.     case ELFCLASSNONE:        /* address size not specified */
  1150.       goto wrong;        /* No support if can't tell address size */
  1151.     case ELFCLASS32:        /* 32-bit addresses */
  1152.       break;
  1153.     case ELFCLASS64:        /* 64-bit addresses */
  1154.       goto wrong;        /* FIXME: 64 bits not yet supported */
  1155.     default:
  1156.       goto wrong;        /* No support if unknown address class */
  1157.     }
  1158.  
  1159.   /* Switch xvec to match the specified byte order.  */
  1160.   switch (x_ehdr.e_ident[EI_DATA])
  1161.     {
  1162.     case ELFDATA2MSB:        /* Big-endian */ 
  1163.       if (!abfd->xvec->header_byteorder_big_p)
  1164.     goto wrong;
  1165.       break;
  1166.     case ELFDATA2LSB:        /* Little-endian */
  1167.       if (abfd->xvec->header_byteorder_big_p)
  1168.     goto wrong;
  1169.       break;
  1170.     case ELFDATANONE:        /* No data encoding specified */
  1171.     default:            /* Unknown data encoding specified */
  1172.       goto wrong;
  1173.     }
  1174.   
  1175.   /* Allocate an instance of the elf_obj_tdata structure and hook it up to
  1176.      the tdata pointer in the bfd. */
  1177.  
  1178.   if (NULL == (elf_tdata (abfd) = (struct elf_obj_tdata *)
  1179.                bfd_zalloc (abfd, sizeof (struct elf_obj_tdata))))
  1180.     {
  1181.       bfd_error = no_memory;
  1182.       return (NULL);
  1183.     }
  1184.  
  1185.   /* FIXME:  Any `wrong' exits below here will leak memory (tdata).  */
  1186.  
  1187.   /* Now that we know the byte order, swap in the rest of the header */
  1188.   i_ehdrp = elf_elfheader (abfd);
  1189.   elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
  1190.   
  1191.   /* If there is no section header table, we're hosed. */
  1192.   if (i_ehdrp->e_shoff == 0)
  1193.     goto wrong;
  1194.  
  1195.   if (i_ehdrp->e_type == ET_EXEC || i_ehdrp->e_type == ET_DYN)
  1196.     abfd -> flags |= EXEC_P;
  1197.  
  1198.   /* Retrieve the architecture information from the xvec and verify
  1199.      that it matches the machine info stored in the ELF header.
  1200.      This allows us to resolve ambiguous formats that might not
  1201.      otherwise be distinguishable. */
  1202.  
  1203.   ebd = (struct elf_backend_data *) (abfd->xvec->backend_data);
  1204.   switch (i_ehdrp->e_machine)
  1205.     {
  1206.     case EM_NONE:
  1207.     case EM_M32:        /* or should this be bfd_arch_obscure? */
  1208.       if (ebd -> arch != bfd_arch_unknown)
  1209.     goto wrong;
  1210.       bfd_default_set_arch_mach(abfd, bfd_arch_unknown, 0);
  1211.       break;
  1212.     case EM_SPARC:
  1213.       if (ebd -> arch != bfd_arch_sparc)
  1214.     goto wrong;
  1215.       bfd_default_set_arch_mach(abfd, bfd_arch_sparc, 0);
  1216.       break;
  1217.     case EM_386:
  1218.       if (ebd -> arch != bfd_arch_i386)
  1219.     goto wrong;
  1220.       bfd_default_set_arch_mach(abfd, bfd_arch_i386, 0);
  1221.       break;
  1222.     case EM_68K:
  1223.       if (ebd -> arch != bfd_arch_m68k)
  1224.     goto wrong;
  1225.       bfd_default_set_arch_mach(abfd, bfd_arch_m68k, 0);
  1226.       break;
  1227.     case EM_88K:
  1228.       if (ebd -> arch != bfd_arch_m88k)
  1229.     goto wrong;
  1230.       bfd_default_set_arch_mach(abfd, bfd_arch_m88k, 0);
  1231.       break;
  1232.     case EM_860:
  1233.       if (ebd -> arch != bfd_arch_i860)
  1234.     goto wrong;
  1235.       bfd_default_set_arch_mach(abfd, bfd_arch_i860, 0);
  1236.       break;
  1237.     case EM_MIPS:
  1238.       if (ebd -> arch != bfd_arch_mips)
  1239.     goto wrong;
  1240.       bfd_default_set_arch_mach(abfd, bfd_arch_mips, 0);
  1241.       break;
  1242.     case EM_HPPA:
  1243.       if (ebd -> arch != bfd_arch_hppa)
  1244.     goto wrong;
  1245.       bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 0);
  1246.       break;
  1247.     default:
  1248.       goto wrong;
  1249.     }
  1250.   
  1251.   /* Allocate space for a copy of the section header table in 
  1252.      internal form, seek to the section header table in the file,
  1253.      read it in, and convert it to internal form.  As a simple sanity
  1254.      check, verify that the what BFD thinks is the size of each section
  1255.      header table entry actually matches the size recorded in the file. */
  1256.  
  1257.   if (i_ehdrp->e_shentsize != sizeof (x_shdr))
  1258.     goto wrong;
  1259.   i_shdrp = (Elf_Internal_Shdr *)
  1260.     bfd_alloc (abfd, sizeof (*i_shdrp) * i_ehdrp->e_shnum);
  1261.   if (! i_shdrp)
  1262.     {
  1263.       bfd_error = no_memory;
  1264.       return (NULL);
  1265.     }
  1266.   if (bfd_seek (abfd, i_ehdrp->e_shoff, SEEK_SET) == -1)
  1267.     {
  1268.       bfd_error = system_call_error;
  1269.       return (NULL);
  1270.     }
  1271.   for (shindex = 0; shindex < i_ehdrp->e_shnum; shindex++)
  1272.     {
  1273.       if (bfd_read ((PTR) &x_shdr, sizeof x_shdr, 1, abfd)
  1274.       != sizeof (x_shdr))
  1275.     {
  1276.       bfd_error = system_call_error;
  1277.       return (NULL);
  1278.     }
  1279.       elf_swap_shdr_in (abfd, &x_shdr, i_shdrp + shindex);
  1280.     }
  1281.  
  1282.   elf_elfsections (abfd) = i_shdrp;
  1283.   
  1284.   /* Read in the string table containing the names of the sections.  We
  1285.      will need the base pointer to this table later. */
  1286.   /* We read this inline now, so that we don't have to go through
  1287.      bfd_section_from_shdr with it (since this particular strtab is
  1288.      used to find all of the ELF section names.) */
  1289.  
  1290.   shstrtab = elf_get_str_section (abfd, i_ehdrp->e_shstrndx);
  1291.   if (! shstrtab)
  1292.     return (NULL);
  1293.   
  1294.   /* Once all of the section headers have been read and converted, we
  1295.      can start processing them.  Note that the first section header is
  1296.      a dummy placeholder entry, so we ignore it.
  1297.  
  1298.      We also watch for the symbol table section and remember the file
  1299.      offset and section size for both the symbol table section and the
  1300.      associated string table section. */
  1301.  
  1302.   for (shindex = 1; shindex < i_ehdrp->e_shnum; shindex++)
  1303.     {
  1304.       bfd_section_from_shdr (abfd, shindex);
  1305.     }
  1306.  
  1307.   /* Remember the entry point specified in the ELF file header. */
  1308.  
  1309.   bfd_get_start_address (abfd) = i_ehdrp->e_entry;
  1310.  
  1311.   return (abfd->xvec);
  1312. }
  1313.  
  1314. /*
  1315.   Takes a bfd and a symbol, returns a pointer to the elf specific area
  1316.   of the symbol if there is one.
  1317.   */
  1318. static elf_symbol_type *
  1319. DEFUN(elf_symbol_from,(ignore_abfd, symbol),
  1320.       bfd            *ignore_abfd AND
  1321.       asymbol        *symbol)
  1322. {
  1323.   if (symbol->the_bfd->xvec->flavour != bfd_target_elf_flavour)
  1324.     return (elf_symbol_type *)NULL;
  1325.  
  1326.   if (symbol->the_bfd->tdata.elf_obj_data == (struct elf_obj_tdata *)NULL)
  1327.     return (elf_symbol_type *)NULL;
  1328.  
  1329.   return  (elf_symbol_type *) symbol;
  1330. }
  1331.  
  1332. /*  Core files are simply standard ELF formatted files that partition
  1333.     the file using the execution view of the file (program header table)
  1334.     rather than the linking view.  In fact, there is no section header
  1335.     table in a core file.
  1336.  
  1337.     The process status information (including the contents of the general
  1338.     register set) and the floating point register set are stored in a
  1339.     segment of type PT_NOTE.  We handcraft a couple of extra bfd sections
  1340.     that allow standard bfd access to the general registers (.reg) and the
  1341.     floating point registers (.reg2).
  1342.  
  1343.  */
  1344.  
  1345. bfd_target *
  1346. DEFUN (elf_core_file_p, (abfd), bfd *abfd)
  1347. {
  1348.   Elf_External_Ehdr x_ehdr;    /* Elf file header, external form */
  1349.   Elf_Internal_Ehdr *i_ehdrp;    /* Elf file header, internal form */
  1350.   Elf_External_Phdr x_phdr;    /* Program header table entry, external form */
  1351.   Elf_Internal_Phdr *i_phdrp;    /* Program header table, internal form */
  1352.   unsigned int phindex;
  1353.   
  1354.   /* Read in the ELF header in external format.  */
  1355.  
  1356.   if (bfd_read ((PTR) &x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr))
  1357.     {
  1358.       bfd_error = system_call_error;
  1359.       return (NULL);
  1360.     }
  1361.  
  1362.   /* Now check to see if we have a valid ELF file, and one that BFD can
  1363.      make use of.  The magic number must match, the address size ('class')
  1364.      and byte-swapping must match our XVEC entry, and it must have a
  1365.      program header table (FIXME: See comments re segments at top of this
  1366.      file). */
  1367.  
  1368.   if (elf_file_p (&x_ehdr) == false)
  1369.     {
  1370.     wrong:
  1371.       bfd_error = wrong_format;
  1372.       return (NULL);
  1373.     }
  1374.  
  1375.   /* FIXME, Check EI_VERSION here !  */
  1376.  
  1377.   switch (x_ehdr.e_ident[EI_CLASS])
  1378.     {
  1379.     case ELFCLASSNONE:        /* address size not specified */
  1380.       goto wrong;        /* No support if can't tell address size */
  1381.     case ELFCLASS32:        /* 32-bit addresses */
  1382.       break;
  1383.     case ELFCLASS64:        /* 64-bit addresses */
  1384.       goto wrong;        /* FIXME: 64 bits not yet supported */
  1385.     default:
  1386.       goto wrong;        /* No support if unknown address class */
  1387.     }
  1388.  
  1389.   /* Switch xvec to match the specified byte order.  */
  1390.   switch (x_ehdr.e_ident[EI_DATA])
  1391.     {
  1392.     case ELFDATA2MSB:        /* Big-endian */ 
  1393.       if (abfd->xvec->byteorder_big_p == false)
  1394.     goto wrong;
  1395.       break;
  1396.     case ELFDATA2LSB:        /* Little-endian */
  1397.       if (abfd->xvec->byteorder_big_p == true)
  1398.     goto wrong;
  1399.       break;
  1400.     case ELFDATANONE:        /* No data encoding specified */
  1401.     default:            /* Unknown data encoding specified */
  1402.       goto wrong;
  1403.     }
  1404.   
  1405.   /* Allocate an instance of the elf_obj_tdata structure and hook it up to
  1406.      the tdata pointer in the bfd. */
  1407.  
  1408.   elf_tdata (abfd) =
  1409.     (struct elf_obj_tdata *) bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
  1410.   if (elf_tdata (abfd) == NULL)
  1411.     {
  1412.       bfd_error = no_memory;
  1413.       return (NULL);
  1414.     }
  1415.  
  1416.   /* FIXME, `wrong' returns from this point onward, leak memory.  */
  1417.  
  1418.   /* Now that we know the byte order, swap in the rest of the header */
  1419.   i_ehdrp = elf_elfheader (abfd);
  1420.   elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
  1421.  
  1422.   /* If there is no program header, or the type is not a core file, then
  1423.      we are hosed. */
  1424.   if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
  1425.     goto wrong;
  1426.  
  1427.   /* Allocate space for a copy of the program header table in 
  1428.      internal form, seek to the program header table in the file,
  1429.      read it in, and convert it to internal form.  As a simple sanity
  1430.      check, verify that the what BFD thinks is the size of each program
  1431.      header table entry actually matches the size recorded in the file. */
  1432.  
  1433.   if (i_ehdrp->e_phentsize != sizeof (x_phdr))
  1434.     goto wrong;
  1435.   i_phdrp = (Elf_Internal_Phdr *)
  1436.     bfd_alloc (abfd, sizeof (*i_phdrp) * i_ehdrp->e_phnum);
  1437.   if (! i_phdrp)
  1438.     {
  1439.       bfd_error = no_memory;
  1440.       return (NULL);
  1441.     }
  1442.   if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) == -1)
  1443.     {
  1444.       bfd_error = system_call_error;
  1445.       return (NULL);
  1446.     }
  1447.   for (phindex = 0; phindex < i_ehdrp->e_phnum; phindex++)
  1448.     {
  1449.       if (bfd_read ((PTR) &x_phdr, sizeof (x_phdr), 1, abfd)
  1450.       != sizeof (x_phdr))
  1451.     {
  1452.       bfd_error = system_call_error;
  1453.       return (NULL);
  1454.     }
  1455.       elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
  1456.     }
  1457.  
  1458.   /* Once all of the program headers have been read and converted, we
  1459.      can start processing them. */
  1460.  
  1461.   for (phindex = 0; phindex < i_ehdrp->e_phnum; phindex++)
  1462.     {
  1463.       bfd_section_from_phdr (abfd, i_phdrp + phindex, phindex);
  1464.       if ((i_phdrp + phindex) -> p_type == PT_NOTE)
  1465.     {
  1466.       elf_corefile_note (abfd, i_phdrp + phindex);
  1467.     }
  1468.     }
  1469.  
  1470.   /* Remember the entry point specified in the ELF file header. */
  1471.  
  1472.   bfd_get_start_address (abfd) = i_ehdrp->e_entry;
  1473.  
  1474.   return (abfd->xvec);
  1475. }
  1476.  
  1477. boolean
  1478. DEFUN (elf_mkobject, (abfd), bfd *abfd)
  1479. {
  1480.   /* this just does initialization */
  1481.   /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
  1482.   elf_tdata(abfd) = (struct elf_obj_tdata *)
  1483.     bfd_zalloc (abfd, sizeof(struct elf_obj_tdata));
  1484.   if (elf_tdata(abfd) == 0) {
  1485.     bfd_error = no_memory;
  1486.     return false;
  1487.   }
  1488.   /* since everything is done at close time, do we need any
  1489.      initialization? */
  1490.   
  1491.   return (true);
  1492. }
  1493.  
  1494. /*
  1495.   Create ELF output from BFD sections.
  1496.   
  1497.   Essentially, just create the section header and forget about the program
  1498.   header for now.   
  1499.   
  1500. */
  1501.  
  1502. /* lacking nested functions and nested types, set up for mapping over
  1503.    BFD sections to produce ELF sections */
  1504.  
  1505. typedef struct {
  1506.   Elf_Internal_Ehdr    *i_ehdr;
  1507.   Elf_Internal_Shdr    *i_shdrp;
  1508.   struct strtab        *shstrtab;
  1509.   int            symtab_section;
  1510. } elf_sect_thunk;
  1511.  
  1512. static int
  1513. elf_idx_of_sym(abfd, sym)
  1514.      bfd *abfd;
  1515.      asymbol *sym;
  1516. {
  1517.   int i;
  1518.   for ( i = 0; i < abfd->symcount; i++ )
  1519.     {
  1520.       if ( sym == (asymbol *)abfd->outsymbols[i] )
  1521.     {
  1522.       /* sanity check */
  1523.       BFD_ASSERT( (strcmp(sym->name, abfd->outsymbols[i]->name) == 0)
  1524.              || (strlen(sym->name) == 0) );
  1525.       return i+1;
  1526.     }
  1527.     }
  1528.   return 0;
  1529. }
  1530.  
  1531. static void
  1532. DEFUN (elf_make_sections, (abfd, asect, obj),
  1533.        bfd        *abfd AND
  1534.        asection        *asect AND
  1535.        PTR        obj)
  1536. {
  1537.   elf_sect_thunk *thunk = (elf_sect_thunk*)obj;
  1538.   /* most of what is in bfd_shdr_from_section goes in here... */
  1539.   /* and all of these sections generate at *least* one ELF section. */
  1540.   int this_section;
  1541.   int idx;
  1542.   
  1543.   /* check if we're making a PROGBITS section... */
  1544.   /* if ((asect->flags & SEC_ALLOC) && (asect->flags & SEC_LOAD)) */
  1545.   /* this was too strict... what *do* we want to check here? */
  1546.   if (1)
  1547.     {
  1548.       Elf_Internal_Shdr *this_hdr;
  1549.       this_section = elf_section_from_bfd_section (abfd, asect);
  1550.       this_hdr = &thunk->i_shdrp[this_section];
  1551.  
  1552.       this_hdr->sh_addr = asect->vma;
  1553.       this_hdr->sh_size = asect->_raw_size;
  1554.       /* contents already set by elf_set_section_contents */
  1555.  
  1556.       if (asect->flags & SEC_RELOC)
  1557.     {
  1558.       /* emit a reloc section, and thus strtab and symtab... */
  1559.       Elf_Internal_Shdr *rela_hdr;
  1560.       Elf_Internal_Shdr *symtab_hdr;
  1561.       Elf_External_Rela *outbound_relocs;
  1562.       int rela_section;
  1563.       
  1564.       symtab_hdr = &thunk->i_shdrp[thunk->symtab_section];
  1565.  
  1566.       if (thunk->symtab_section == this_section + 1)
  1567.         rela_section = thunk->symtab_section + 2; /* symtab + symstrtab */
  1568.       else
  1569.         rela_section = this_section + 1;
  1570.       rela_hdr = &thunk->i_shdrp[rela_section];
  1571.       rela_hdr->sh_type = SHT_RELA;
  1572.       rela_hdr->sh_link = thunk->symtab_section;
  1573.       rela_hdr->sh_info = this_section;
  1574.       rela_hdr->sh_entsize = sizeof (Elf_External_Rela);
  1575.       /* orelocation has the data, reloc_count has the count... */
  1576.       rela_hdr->sh_size = rela_hdr->sh_entsize * asect->reloc_count;
  1577.       outbound_relocs = (Elf_External_Rela *)
  1578.         bfd_alloc(abfd, asect->reloc_count * sizeof(Elf_External_Rela));
  1579.       for (idx = 0; idx < asect->reloc_count; idx++)
  1580.         {
  1581.           Elf_Internal_Rela dst;
  1582.           arelent        *ptr;
  1583.           Elf_External_Rela  *src;
  1584.  
  1585.           ptr = asect->orelocation[idx];
  1586.           src = outbound_relocs + idx;
  1587.           if (asect->flags & SEC_RELOC)
  1588.         dst.r_offset = ptr->address - asect->vma;
  1589.           else
  1590.         dst.r_offset = ptr->address;
  1591.  
  1592.           /* @@ This assumes the symbols were written (or will be
  1593.          written) in the same order that they appear in
  1594.          abfd->outsymbols.  */
  1595.           if (ptr->sym_ptr_ptr && ptr->sym_ptr_ptr[0])
  1596.         dst.r_info = ELF_R_INFO (elf_idx_of_sym (abfd,
  1597.                              ptr->sym_ptr_ptr[0]),
  1598.                      ptr->howto->type);
  1599.           else
  1600.         dst.r_info = ELF_R_INFO (STN_UNDEF, ptr->howto->type);
  1601.  
  1602.           dst.r_addend = ptr->addend;
  1603.           elf_swap_reloca_out(abfd, &dst, src);
  1604.         }
  1605.       rela_hdr->contents = (void*)outbound_relocs;
  1606.     }
  1607.       if (asect->flags & SEC_ALLOC)
  1608.         {
  1609.           this_hdr->sh_flags |= SHF_ALLOC;
  1610.           if (asect->flags & SEC_LOAD)
  1611.             {
  1612.           /* @@ Do something with sh_type? */
  1613.         }
  1614.         }
  1615.       if (!(asect->flags & SEC_READONLY)) 
  1616.         this_hdr->sh_flags |= SHF_WRITE;
  1617.  
  1618.       if (asect->flags & SEC_CODE)
  1619.         this_hdr->sh_flags |= SHF_EXECINSTR;
  1620.     }
  1621. }
  1622.  
  1623. static void
  1624. DEFUN (elf_fake_sections, (abfd, asect, obj),
  1625.        bfd        *abfd AND
  1626.        asection        *asect AND
  1627.        PTR        obj)
  1628. {
  1629.   elf_sect_thunk *thunk = (elf_sect_thunk*)obj;
  1630.   /* most of what is in bfd_shdr_from_section goes in here... */
  1631.   /* and all of these sections generate at *least* one ELF section. */
  1632.   int this_section;
  1633.  
  1634.   /* check if we're making a PROGBITS section... */
  1635.   /* if ((asect->flags & SEC_ALLOC) && (asect->flags & SEC_LOAD)) */
  1636.   /* this was too strict... what *do* we want to check here? */
  1637.   if (1)
  1638.     {
  1639.       Elf_Internal_Shdr *this_hdr;
  1640.       this_section = thunk->i_ehdr->e_shnum++;
  1641.       this_hdr = &thunk->i_shdrp[this_section];
  1642.       this_hdr->sh_name = 
  1643.     bfd_add_to_strtab (abfd, thunk->shstrtab, asect->name);
  1644.       /* we need to log the type *now* so that elf_section_from_bfd_section
  1645.      can find us... have to set rawdata too. */
  1646.       this_hdr->rawdata = (void*)asect;
  1647.       if ((asect->flags & SEC_ALLOC) && (asect->flags & SEC_LOAD))
  1648.     this_hdr->sh_type = SHT_PROGBITS;
  1649.       /* @@ Select conditions correctly!  */
  1650.       else if (!strcmp (asect->name, ".bss"))
  1651.     this_hdr->sh_type = SHT_NOBITS;
  1652.       else
  1653.     /* what *do* we put here? */
  1654.     this_hdr->sh_type = SHT_PROGBITS;
  1655.  
  1656.  
  1657.       {
  1658.     /* Emit a strtab and symtab, and possibly a reloc section.  */
  1659.     Elf_Internal_Shdr *rela_hdr;
  1660.     Elf_Internal_Shdr *symtab_hdr;
  1661.     Elf_Internal_Shdr *symstrtab_hdr;
  1662.     int rela_section;
  1663.     int symstrtab_section;
  1664.  
  1665.     /* Note that only one symtab is used, so just remember it
  1666.        for now.  */
  1667.     if (! thunk->symtab_section)
  1668.       {
  1669.         thunk->symtab_section = thunk->i_ehdr->e_shnum++;
  1670.         symtab_hdr = &thunk->i_shdrp[thunk->symtab_section];
  1671.         symtab_hdr->sh_name =
  1672.           bfd_add_to_strtab (abfd, thunk->shstrtab, ".symtab");
  1673.         symtab_hdr->sh_type = SHT_SYMTAB;
  1674.         symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
  1675.  
  1676.         symstrtab_section = thunk->i_ehdr->e_shnum++;
  1677.         BFD_ASSERT(symstrtab_section == thunk->symtab_section+1);
  1678.         symstrtab_hdr = &thunk->i_shdrp[symstrtab_section];
  1679.         symtab_hdr->sh_link = symstrtab_section;
  1680.         symstrtab_hdr->sh_name =
  1681.           bfd_add_to_strtab (abfd, thunk->shstrtab, ".strtab");
  1682.         symstrtab_hdr->sh_type = SHT_STRTAB;
  1683.  
  1684.         symtab_hdr->contents = 0;
  1685.         symstrtab_hdr->contents = 0;
  1686.         symstrtab_hdr->sh_size = 0;
  1687.       } 
  1688.     else
  1689.       symtab_hdr = &thunk->i_shdrp[thunk->symtab_section];
  1690.  
  1691.     if (asect->flags & SEC_RELOC)
  1692.       {
  1693.         rela_section = thunk->i_ehdr->e_shnum++;
  1694.         rela_hdr = &thunk->i_shdrp[rela_section];
  1695.         rela_hdr->sh_name =
  1696.           bfd_add_2_to_strtab (abfd, thunk->shstrtab, ".rela",
  1697.                    asect->name);
  1698.         rela_hdr->sh_type = SHT_RELA;
  1699.         rela_hdr->sh_link = thunk->symtab_section;
  1700.         rela_hdr->sh_info = this_section;
  1701.         rela_hdr->sh_entsize = sizeof (Elf_External_Rela);
  1702.       }
  1703.       }
  1704.       if (asect->flags & SEC_ALLOC)
  1705.     {
  1706.       this_hdr->sh_flags |= SHF_ALLOC;
  1707.       if (asect->flags & SEC_LOAD)
  1708.         {
  1709.           /* @@ Do something with sh_type?  */
  1710.         }
  1711.     }
  1712.       if (!(asect->flags & SEC_READONLY))
  1713.     this_hdr->sh_flags |= SHF_WRITE;
  1714.       if (asect->flags & SEC_CODE)
  1715.     this_hdr->sh_flags |= SHF_EXECINSTR;
  1716.     }
  1717. }
  1718.  
  1719.  
  1720. static boolean
  1721. DEFUN (elf_compute_section_file_positions, (abfd), bfd *abfd)
  1722. {
  1723.   Elf_Internal_Ehdr *i_ehdrp;    /* Elf file header, internal form */
  1724.   Elf_Internal_Shdr *i_shdrp;    /* Section header table, internal form */
  1725.   struct strtab *shstrtab;
  1726.   int count, maxsections;
  1727.   elf_sect_thunk est;
  1728.  
  1729.   if (! elf_shstrtab (abfd)) {
  1730.     i_ehdrp = elf_elfheader (abfd);    /* build new header in tdata memory */
  1731.     shstrtab = bfd_new_strtab(abfd);
  1732.     
  1733.     i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
  1734.     i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
  1735.     i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
  1736.     i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
  1737.  
  1738.     i_ehdrp->e_ident[EI_CLASS] = ELFCLASS32; /* FIXME: find out from bfd */
  1739.     i_ehdrp->e_ident[EI_DATA] =
  1740.       abfd->xvec->byteorder_big_p ? ELFDATA2MSB : ELFDATA2LSB;
  1741.     i_ehdrp->e_ident[EI_VERSION] = EV_CURRENT;
  1742.  
  1743.     for(count = EI_PAD; count < EI_NIDENT; count ++)
  1744.       i_ehdrp->e_ident[count] = 0;
  1745.       
  1746.     i_ehdrp->e_type = (abfd->flags & EXEC_P)? ET_EXEC : ET_REL;
  1747.     switch(bfd_get_arch(abfd))
  1748.       {
  1749.       case bfd_arch_unknown:
  1750.     i_ehdrp->e_machine = EM_NONE;
  1751.     break;
  1752.       case bfd_arch_sparc:
  1753.     i_ehdrp->e_machine = EM_SPARC;
  1754.     break;
  1755.       case bfd_arch_i386:
  1756.     i_ehdrp->e_machine = EM_386;
  1757.     break;
  1758.       case bfd_arch_m68k:
  1759.     i_ehdrp->e_machine = EM_68K;
  1760.     break;
  1761.       case bfd_arch_m88k:
  1762.     i_ehdrp->e_machine = EM_88K;
  1763.     break;
  1764.       case bfd_arch_i860:
  1765.     i_ehdrp->e_machine = EM_860;
  1766.     break;
  1767.       case bfd_arch_mips:          /* MIPS Rxxxx */
  1768.     i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */
  1769.     break;
  1770.       case bfd_arch_hppa:
  1771.     i_ehdrp->e_machine = EM_HPPA;
  1772.     break;
  1773.     /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
  1774.       default:
  1775.     i_ehdrp->e_machine = EM_NONE;
  1776.       }
  1777.     i_ehdrp->e_version = EV_CURRENT;
  1778.     i_ehdrp->e_ehsize = sizeof(Elf_External_Ehdr);
  1779.     
  1780.     /* no program header, for now. */
  1781.     i_ehdrp->e_phoff = 0;
  1782.     i_ehdrp->e_phentsize = 0;
  1783.     i_ehdrp->e_phnum = 0;
  1784.  
  1785.     /* each bfd section is section header entry */
  1786.     i_ehdrp->e_entry = bfd_get_start_address (abfd);
  1787.     i_ehdrp->e_shentsize = sizeof (Elf_External_Shdr);
  1788.  
  1789.     /* figure at most each section can have a rel, strtab, symtab */
  1790.     maxsections = 4*bfd_count_sections(abfd)+2;
  1791.  
  1792.     i_ehdrp->e_shoff = i_ehdrp->e_ehsize;
  1793.  
  1794.     /* and we'll just have to fix up the offsets later. */
  1795.     /* outbase += i_ehdr.e_shentsize * i_ehdr.e_shnum; */
  1796.     
  1797.     i_shdrp = (Elf_Internal_Shdr *)
  1798.       bfd_alloc (abfd, sizeof (*i_shdrp) * maxsections);
  1799.     if (! i_shdrp)
  1800.       {
  1801.     bfd_error = no_memory;
  1802.     return (false);
  1803.       }
  1804.     for (count=0; count < maxsections; count++) 
  1805.       {
  1806.     i_shdrp[count].rawdata = 0;
  1807.     i_shdrp[count].contents = 0;
  1808.       }
  1809.     
  1810.     
  1811.     i_shdrp[0].sh_name = 0;
  1812.     i_shdrp[0].sh_type = SHT_NULL;
  1813.     i_shdrp[0].sh_flags = 0;
  1814.     i_shdrp[0].sh_addr = 0;
  1815.     i_shdrp[0].sh_offset = 0;
  1816.     i_shdrp[0].sh_size = 0;
  1817.     i_shdrp[0].sh_link = SHN_UNDEF;
  1818.     i_shdrp[0].sh_info = 0;
  1819.     i_shdrp[0].sh_addralign = 0;
  1820.     i_shdrp[0].sh_entsize = 0;
  1821.  
  1822.     i_ehdrp->e_shnum = 1;
  1823.  
  1824.     elf_elfsections (abfd) = i_shdrp;
  1825.     elf_shstrtab (abfd) = shstrtab;
  1826.   }
  1827.   est.i_ehdr = elf_elfheader(abfd);
  1828.   est.i_shdrp = elf_elfsections(abfd);
  1829.   est.shstrtab = elf_shstrtab(abfd);
  1830.   est.symtab_section = 0;    /* elf_fake_sections fils it in */
  1831.  
  1832.   bfd_map_over_sections(abfd, elf_fake_sections, &est);
  1833.   elf_onesymtab (abfd) = est.symtab_section;
  1834.   return (true);
  1835. }
  1836.  
  1837. static boolean
  1838. DEFUN (elf_write_phdrs, (abfd, i_ehdrp, i_phdrp, phdr_cnt),
  1839.        bfd            *abfd AND
  1840.        Elf_Internal_Ehdr    *i_ehdrp AND
  1841.        Elf_Internal_Phdr    *i_phdrp AND
  1842.        Elf_Half            phdr_cnt)
  1843. {
  1844.     /* first program header entry goes after the file header */
  1845.     int outbase = i_ehdrp->e_ehsize;
  1846.     int i;
  1847.     Elf_External_Phdr    x_phdr;
  1848.  
  1849.     for ( i = 0; i < phdr_cnt; i++ ) {
  1850.         elf_swap_phdr_out(abfd, i_phdrp + i, &x_phdr);
  1851.         bfd_seek(abfd, outbase, SEEK_SET);
  1852.         bfd_write( (PTR)&x_phdr, sizeof(x_phdr), 1, abfd);
  1853.         outbase += sizeof(x_phdr);
  1854.     }
  1855.  
  1856.     return true;
  1857. }
  1858.  
  1859. static Elf_Internal_Phdr *
  1860. DEFUN (elf_build_phdrs, (abfd, i_ehdrp, i_shdrp, phdr_cnt),
  1861.        bfd            *abfd AND
  1862.        Elf_Internal_Ehdr    *i_ehdrp AND
  1863.        Elf_Internal_Shdr    *i_shdrp AND
  1864.        Elf_Half            *phdr_cnt)
  1865. {
  1866.   Elf_Internal_Phdr *phdr_buf;
  1867.   int idx;
  1868.   /* NOTES:
  1869.      1. The program header table is *not* loaded as part
  1870.         of the memory image of the program.  If this
  1871.     changes later, the PT_PHDR entry must come first.
  1872.      2. there is currently no support for program header
  1873.      entries of type PT_PHDR, PT_DYNAMIC, PT_INTERP,
  1874.      or PT_SHLIB. */
  1875.  
  1876.   /* A. Figure out how many program header table entries are needed
  1877.        1. PT_LOAD for the text segment
  1878.        2. PT_LOAD for the data segment
  1879.      Then, reserve space for one more pointer.  This will be NULL
  1880.      to indicate the end of the program header table.  */
  1881.  
  1882. #ifdef PHDRS_INCLUDED
  1883.   *phdr_cnt = 4;
  1884. #else
  1885.   /* XXX right now, execve() expects exactly 3 PT entries on HPPA-OSF.  */
  1886.   *phdr_cnt = 3;
  1887. #endif
  1888.  
  1889.   phdr_buf = (Elf_Internal_Phdr *)bfd_xmalloc(((*phdr_cnt) + 1)
  1890.                           *
  1891.                           sizeof(Elf_Internal_Phdr));
  1892.  
  1893.   idx = 0;
  1894. #ifdef PHDRS_INCLUDED
  1895.   /* B. Fill in the PT_PHDR entry. */
  1896.  
  1897.   idx++;
  1898. #endif    
  1899.  
  1900.   /* C. Fill in the PT_LOAD entry for the text segment. */
  1901.  
  1902.   phdr_buf[idx].p_type    = PT_LOAD;
  1903.  
  1904.   /* get virtual/physical address from .text section */
  1905.   phdr_buf[idx].p_vaddr    = bfd_get_section_by_name(abfd,".text")->vma;
  1906.   phdr_buf[idx].p_paddr    = 0;    /* XXX */
  1907.  
  1908.   /* Ultimately, we would like the size of the .text load
  1909.      segment to be the sum of the following sections:
  1910.     the program header table itself
  1911.     .interp
  1912.     .hash
  1913.     .dynsym
  1914.     .dynstr
  1915.     .rela.bss
  1916.     .rela.plt
  1917.     .init
  1918.     .text
  1919.     .fini
  1920.     .rodata
  1921.      But, right now, it will be the sum of the following sections:
  1922.     .text
  1923.     .rodata */
  1924.  
  1925.   {
  1926.     static char *CONST ld_sect_names[] = { ".text", ".rodata", NULL };
  1927.     int i;
  1928.     int ld_size = 0;
  1929.  
  1930.     for ( i = 0; ld_sect_names[i]; i++ )
  1931.       {
  1932.     asection *asect = bfd_get_section_by_name(abfd,
  1933.                           ld_sect_names[i]);
  1934.  
  1935.     if ( asect )
  1936.       ld_size += bfd_section_size(abfd, asect);
  1937.       }
  1938.     phdr_buf[idx].p_filesz = ld_size;
  1939.     /* XXX: need to fix this */
  1940.     phdr_buf[idx].p_memsz = ld_size;
  1941.   }
  1942.   phdr_buf[idx].p_flags = PF_R + PF_X;        
  1943.   phdr_buf[idx].p_align =
  1944.     bfd_get_section_by_name(abfd,".text")->alignment_power;
  1945.  
  1946.   idx++;
  1947.  
  1948.   /* D. Fill in the PT_LOAD entry for the data segment.    */
  1949.  
  1950.   phdr_buf[idx].p_type    = PT_LOAD;
  1951.  
  1952.   /* get virtual/physical address from .data section */
  1953.   phdr_buf[idx].p_vaddr    = bfd_get_section_by_name(abfd,".data")->vma;
  1954.   phdr_buf[idx].p_paddr    = 0;    /* XXX */
  1955.  
  1956.   /* Ultimately, we would like the size of the data load segment
  1957.      to be the sum of the following sections:
  1958.     the PT_DYNAMIC program header table entry
  1959.     .plt
  1960.     .data
  1961.     .data1
  1962.     .got
  1963.     .dynamic
  1964.      But, right now, it will be the sum of the following sections:
  1965.     .data */
  1966.  
  1967.   {
  1968.     static char *CONST ld_sect_names[] = { ".data", NULL };
  1969.     int i;
  1970.     int ld_size = 0;
  1971.  
  1972.     for ( i = 0; ld_sect_names[i]; i++ )
  1973.       {
  1974.     asection *asect = bfd_get_section_by_name(abfd,
  1975.                           ld_sect_names[i]);
  1976.  
  1977.     if ( asect )
  1978.       ld_size += bfd_section_size(abfd, asect);
  1979.       }
  1980.     phdr_buf[idx].p_filesz = ld_size;
  1981.     /* XXX: need to fix this */
  1982.     phdr_buf[idx].p_memsz = ld_size;
  1983.   }
  1984.   phdr_buf[idx].p_flags = PF_R + PF_W + PF_X;        
  1985.   phdr_buf[idx].p_align
  1986.     = bfd_get_section_by_name(abfd,".data")->alignment_power;
  1987.  
  1988.   idx++;
  1989.  
  1990.   /* E. Fill in the PT_LOAD entry for the bss segment.    */
  1991.  
  1992.   phdr_buf[idx].p_type    = PT_LOAD;
  1993.  
  1994.   /* get virtual/physical address from .data section */
  1995.   phdr_buf[idx].p_vaddr    = bfd_get_section_by_name(abfd,".bss")->vma;
  1996.   phdr_buf[idx].p_paddr    = 0;    /* XXX */
  1997.  
  1998.   {
  1999.     static char *CONST ld_sect_names[] = { ".bss", NULL };
  2000.     int i;
  2001.     int ld_size = 0;
  2002.  
  2003.     for ( i = 0; ld_sect_names[i]; i++ )
  2004.       {
  2005.     asection *asect = bfd_get_section_by_name(abfd,
  2006.                           ld_sect_names[i]);
  2007.  
  2008.     if ( asect )
  2009.       ld_size += bfd_section_size(abfd, asect);
  2010.       }
  2011.     phdr_buf[idx].p_filesz = 0;
  2012.     /* XXX: need to fix this */
  2013.     phdr_buf[idx].p_memsz = ld_size;
  2014.   }
  2015.   phdr_buf[idx].p_flags = PF_R + PF_W + PF_X;        
  2016.   phdr_buf[idx].p_align
  2017.     = bfd_get_section_by_name(abfd,".bss")->alignment_power;
  2018.  
  2019.   idx++;
  2020.  
  2021.   /* F. Set up the "end of program header table" sentinel.    */
  2022.  
  2023.   bzero((char *)(phdr_buf+idx),sizeof(Elf_Internal_Phdr));
  2024.   idx++;
  2025.  
  2026.   BFD_ASSERT(idx - 1 == *phdr_cnt);
  2027.  
  2028.   return phdr_buf;    
  2029. }
  2030.  
  2031. boolean
  2032. DEFUN (elf_write_object_contents, (abfd), bfd *abfd)
  2033. {
  2034.   Elf_External_Ehdr x_ehdr;    /* Elf file header, external form */
  2035.   Elf_Internal_Ehdr *i_ehdrp;    /* Elf file header, internal form */
  2036.   Elf_External_Phdr *x_phdrp;    /* Program header table, external form */
  2037.   Elf_Internal_Phdr *i_phdrp;    /* Program header table, internal form */
  2038.   Elf_External_Shdr *x_shdrp;    /* Section header table, external form */
  2039.   Elf_Internal_Shdr *i_shdrp;    /* Section header table, internal form */
  2040.   asection *nsect;
  2041.   elf_sect_thunk est;
  2042.   
  2043.   int outbase = 0;
  2044.   int count;
  2045.   int scnt;
  2046.   struct strtab *shstrtab;
  2047.   
  2048.   if(abfd->output_has_begun == false)
  2049.     {
  2050.       elf_compute_section_file_positions(abfd);
  2051.       abfd->output_has_begun = true;
  2052.     }
  2053.  
  2054.   i_ehdrp = elf_elfheader (abfd);
  2055.   i_shdrp = elf_elfsections (abfd);
  2056.   shstrtab = elf_shstrtab (abfd);
  2057.  
  2058.   est.i_ehdr = i_ehdrp;
  2059.   est.i_shdrp = i_shdrp;
  2060.   est.shstrtab = shstrtab;
  2061.   est.symtab_section = elf_onesymtab (abfd); /* filled in by elf_fake */
  2062.  
  2063.   bfd_map_over_sections(abfd, elf_make_sections, &est);
  2064.  
  2065.   /* Dump out the symtabs. */
  2066.   {
  2067.     int symcount = bfd_get_symcount (abfd);
  2068.     asymbol ** syms = bfd_get_outsymbols (abfd);
  2069.     struct strtab * stt = bfd_new_strtab (abfd);
  2070.     Elf_Internal_Shdr *symtab_hdr;
  2071.     Elf_Internal_Shdr *symstrtab_hdr;
  2072.     int symstrtab_section;
  2073.     Elf_External_Sym *outbound_syms;
  2074.     int idx;
  2075.  
  2076.     symtab_hdr = &i_shdrp[est.symtab_section];
  2077.     symtab_hdr->sh_type = SHT_SYMTAB;
  2078.     symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
  2079.     symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
  2080.  
  2081.     /* see assert in elf_fake_sections that supports this: */
  2082.     symstrtab_section = est.symtab_section+1;
  2083.     symstrtab_hdr = &i_shdrp[symstrtab_section];
  2084.     symtab_hdr->sh_link = symstrtab_section;
  2085.     symstrtab_hdr->sh_type = SHT_STRTAB;
  2086.  
  2087.     outbound_syms = (Elf_External_Sym*)
  2088.       bfd_alloc(abfd, (1+symcount) * sizeof(Elf_External_Sym));
  2089.     /* now generate the data (for "contents") */
  2090.     for (idx = 0; idx < symcount; idx++)
  2091.       {
  2092.     Elf_Internal_Sym sym;
  2093.     bfd_vma value = syms[idx]->value;
  2094.  
  2095.     sym.st_name = bfd_add_to_strtab (abfd, stt, syms[idx]->name);
  2096.  
  2097.     value += syms[idx]->section->output_section->vma
  2098.         + syms[idx]->section->output_offset;
  2099.     sym.st_value = value;
  2100.  
  2101.     sym.st_size = (Elf_Word)(elf_symbol_from(abfd, syms[idx]))->internal_elf_sym.st_size;
  2102.  
  2103.     if (syms[idx]->flags & BSF_WEAK)
  2104.       sym.st_info = ELF_ST_INFO(STB_WEAK, STT_OBJECT);
  2105.     else if (syms[idx]->flags & BSF_LOCAL) {
  2106.         if ( syms[idx]->flags & BSF_FUNCTION )
  2107.             sym.st_info = ELF_ST_INFO(STB_LOCAL, STT_FUNC);
  2108.         else
  2109.             sym.st_info = ELF_ST_INFO(STB_LOCAL, STT_OBJECT);
  2110.     }
  2111.     else if (syms[idx]->flags & BSF_GLOBAL) {
  2112.         if ( syms[idx]->flags & BSF_FUNCTION )
  2113.             sym.st_info = ELF_ST_INFO(STB_GLOBAL, STT_FUNC);
  2114.         else
  2115.             sym.st_info = ELF_ST_INFO(STB_GLOBAL, STT_OBJECT);
  2116.     }
  2117.     else if (syms[idx]->flags & BSF_EXPORT) {
  2118.         if ( syms[idx]->flags & BSF_FUNCTION )
  2119.             sym.st_info = ELF_ST_INFO(STB_GLOBAL, STT_FUNC);
  2120.         else
  2121.             sym.st_info = ELF_ST_INFO(STB_GLOBAL, STT_OBJECT);
  2122.     }
  2123.     else if (syms[idx]->flags & BSF_SECTION_SYM)
  2124.       sym.st_info = ELF_ST_INFO(STB_LOCAL, STT_SECTION);
  2125.     else if (syms[idx]->flags & BSF_FILE)
  2126.       sym.st_info = ELF_ST_INFO(STB_LOCAL, STT_FILE);
  2127.     else
  2128.       sym.st_info = ELF_ST_INFO(STB_LOCAL, STT_OBJECT);
  2129.  
  2130.     sym.st_other = 0;
  2131.     if (syms[idx]->section) 
  2132.       sym.st_shndx =
  2133.         elf_section_from_bfd_section(abfd,
  2134.                      syms[idx]->section->output_section);
  2135.     else
  2136.       sym.st_shndx = SHN_UNDEF;
  2137.  
  2138.     elf_swap_symbol_out (abfd, &sym, outbound_syms+idx+1);
  2139.       }
  2140.     {
  2141.       /* fill in 0th symbol */
  2142.       Elf_Internal_Sym sym;
  2143.       sym.st_name = 0;
  2144.       sym.st_value = 0;
  2145.       sym.st_size = 0;
  2146.       sym.st_info = 0;
  2147.       sym.st_other = 0;
  2148.       sym.st_shndx = SHN_UNDEF;
  2149.       elf_swap_symbol_out (abfd, &sym, outbound_syms);
  2150.     }
  2151.     symtab_hdr->contents = (void*)outbound_syms;
  2152.     symstrtab_hdr->contents = (void*)stt->tab;
  2153.     symstrtab_hdr->sh_size = stt->length;
  2154.     symstrtab_hdr->sh_type = SHT_STRTAB;
  2155.   }
  2156.  
  2157.   /* put the strtab out too... */
  2158.   {
  2159.     Elf_Internal_Shdr *this_hdr;
  2160.     int this_section;
  2161.  
  2162.     this_section = i_ehdrp->e_shnum++;
  2163.     i_ehdrp->e_shstrndx = this_section;
  2164.     this_hdr = &i_shdrp[this_section];
  2165.     this_hdr->sh_name = bfd_add_to_strtab (abfd, shstrtab, ".shstrtab");
  2166.     this_hdr->sh_type = SHT_STRTAB;
  2167.     this_hdr->sh_size = shstrtab->length;
  2168.     this_hdr->sh_type = SHT_STRTAB;
  2169.     this_hdr->contents = (void*)shstrtab->tab;
  2170.   }
  2171.  
  2172.   outbase = i_ehdrp->e_ehsize;
  2173.  
  2174.   /* if we're building an executable, we'll need a program header table */
  2175.   if (abfd->flags & EXEC_P)
  2176.     {
  2177.       i_ehdrp->e_phentsize = sizeof(Elf_External_Phdr);
  2178.  
  2179.       /* elf_build_phdrs() returns a (NULL-terminated) array of
  2180.      Elf_Internal_Phdrs */
  2181.       i_phdrp = elf_build_phdrs(abfd,i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
  2182.       i_ehdrp->e_phoff = i_ehdrp->e_ehsize;
  2183.       i_ehdrp->e_shoff = i_ehdrp->e_phoff + (i_ehdrp->e_phentsize
  2184.                          * i_ehdrp->e_phnum);
  2185.     }
  2186.  
  2187.   /* swap the header before spitting it out... */
  2188.   elf_swap_ehdr_out (abfd, i_ehdrp, &x_ehdr);
  2189.   bfd_seek (abfd, (file_ptr) 0, SEEK_SET);
  2190.   bfd_write ((PTR) &x_ehdr, sizeof(x_ehdr), 1, abfd);
  2191.  
  2192.   outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
  2193.   outbase += i_ehdrp->e_shentsize * i_ehdrp->e_shnum;
  2194.  
  2195.   /* now we fix up the offsets... */
  2196.   for (count = 1; count < i_ehdrp->e_shnum; count ++)
  2197.     {
  2198.       i_shdrp[count].sh_offset = outbase;
  2199.       outbase += i_shdrp[count].sh_size;
  2200.     }
  2201.  
  2202.   /* If we're building an executable, fixup the program header table
  2203.      offsets.
  2204.  
  2205.      @@ For now, assume that the entries are in a fixed order: text,
  2206.      data, bss.  FIXME */
  2207.  
  2208.   if ( abfd->flags & EXEC_P )
  2209.     {
  2210.       static char *CONST section_name[] = { ".text", ".data", ".bss" };
  2211.  
  2212.       for ( count = 0; count < 3; count ++ )
  2213.     {
  2214.       asection *asect = bfd_get_section_by_name(abfd, section_name[count]);
  2215.       int sh_idx = elf_section_from_bfd_section(abfd, asect);
  2216.  
  2217.       i_phdrp[count].p_offset = i_shdrp[sh_idx].sh_offset;
  2218.     }
  2219.  
  2220.       /* write out the program header table entries */
  2221.       elf_write_phdrs(abfd, i_ehdrp, i_phdrp, i_ehdrp->e_phnum);
  2222.     }
  2223.  
  2224.   /* at this point we've concocted all the ELF sections... */
  2225.   x_shdrp = (Elf_External_Shdr *)
  2226.     bfd_alloc (abfd, sizeof (*x_shdrp) * (i_ehdrp->e_shnum));
  2227.   if (! x_shdrp)
  2228.     {
  2229.       bfd_error = no_memory;
  2230.       return (false);
  2231.     }
  2232.  
  2233.   for (count = 0, scnt = 0; count < i_ehdrp->e_shnum; count++)
  2234.     {
  2235.       elf_swap_shdr_out (abfd, i_shdrp+count, x_shdrp+scnt);
  2236.       scnt++;
  2237.     }
  2238.   bfd_write ((PTR) x_shdrp, sizeof(*x_shdrp), i_ehdrp->e_shnum, abfd);
  2239.   /* need to dump the string table too... */
  2240.   
  2241.   /* after writing the headers, we need to write the sections too... */
  2242.   nsect = abfd->sections;
  2243.   for (count = 0; count < i_ehdrp->e_shnum; count ++)
  2244.     {
  2245.       if(i_shdrp[count].contents)
  2246.     {
  2247.       bfd_seek (abfd, i_shdrp[count].sh_offset, SEEK_SET);
  2248.       bfd_write (i_shdrp[count].contents, i_shdrp[count].sh_size, 1, abfd);
  2249.     }
  2250.     }
  2251.   
  2252.   return true;
  2253. }
  2254.  
  2255. /* Given an index of a section, retrieve a pointer to it.  Note
  2256.    that for our purposes, sections are indexed by {1, 2, ...} with
  2257.    0 being an illegal index. */
  2258.  
  2259. /* In the original, each ELF section went into exactly one BFD
  2260.    section. This doesn't really make sense, so we need a real mapping.
  2261.    The mapping has to hide in the Elf_Internal_Shdr since asection
  2262.    doesn't have anything like a tdata field... */
  2263.    
  2264. static struct sec *
  2265. DEFUN (section_from_elf_index, (abfd, index),
  2266.        bfd            *abfd AND
  2267.        int             index)
  2268. {
  2269.   /* @@ Is bfd_com_section really correct in all the places it could
  2270.      be returned from this routine?  */
  2271.  
  2272.   if (index == SHN_ABS)
  2273.     return &bfd_com_section;
  2274.   if (index == SHN_COMMON)
  2275.     return &bfd_com_section;
  2276.  
  2277.   {
  2278.     Elf_Internal_Shdr *i_shdrp = elf_elfsections (abfd);
  2279.     Elf_Internal_Shdr *hdr = i_shdrp + index;
  2280.  
  2281.     switch (hdr->sh_type)
  2282.       {
  2283.     /* ELF sections that map to BFD sections */
  2284.       case SHT_PROGBITS:
  2285.       case SHT_NOBITS:
  2286.     if (! hdr->rawdata)
  2287.       bfd_section_from_shdr (abfd, index);
  2288.     return (struct sec *) hdr->rawdata;
  2289.  
  2290.       default:
  2291.     return (struct sec *) &bfd_abs_section;
  2292.       }
  2293.   }
  2294. }
  2295.  
  2296. /* given a section, search the header to find them... */
  2297. static int
  2298. DEFUN (elf_section_from_bfd_section, (abfd, asect),
  2299.        bfd        *abfd AND
  2300.        struct sec    *asect)
  2301. {
  2302.   Elf_Internal_Shdr *i_shdrp = elf_elfsections (abfd);
  2303.   int index;
  2304.   Elf_Internal_Shdr *hdr;
  2305.   int maxindex = elf_elfheader (abfd)->e_shnum;
  2306.  
  2307.   if (asect == &bfd_abs_section)
  2308.     return SHN_ABS;
  2309.   if (asect == &bfd_com_section)
  2310.     return SHN_COMMON;
  2311.  
  2312.   for(index = 0; index < maxindex; index++) {
  2313.     hdr = &i_shdrp[index];
  2314.     switch (hdr->sh_type)
  2315.       {
  2316.     /* ELF sections that map to BFD sections */
  2317.       case SHT_PROGBITS:
  2318.       case SHT_NOBITS:
  2319.     if (hdr->rawdata) 
  2320.       {
  2321.         if (((struct sec *)(hdr->rawdata)) == asect)
  2322.           return index;
  2323.       }
  2324.     break;
  2325.       default:
  2326.     break;
  2327.       }
  2328.   }
  2329.   return 0;
  2330. }
  2331.  
  2332. static boolean
  2333. DEFUN (elf_slurp_symbol_table, (abfd, symptrs),
  2334.        bfd        *abfd AND
  2335.        asymbol        **symptrs)    /* Buffer for generated bfd symbols */
  2336. {
  2337.   Elf_Internal_Shdr *i_shdrp = elf_elfsections (abfd);
  2338.   Elf_Internal_Shdr *hdr = i_shdrp + elf_onesymtab (abfd);
  2339.   int symcount;        /* Number of external ELF symbols */
  2340.   int i;
  2341.   elf_symbol_type *sym;        /* Pointer to current bfd symbol */
  2342.   elf_symbol_type *symbase;    /* Buffer for generated bfd symbols */
  2343.   Elf_Internal_Sym i_sym;
  2344.   Elf_External_Sym *x_symp;
  2345.  
  2346.   /* this is only valid because there is only one symtab... */
  2347.   /* FIXME:  This is incorrect, there may also be a dynamic symbol
  2348.      table which is a subset of the full symbol table.  We either need
  2349.      to be prepared to read both (and merge them) or ensure that we
  2350.      only read the full symbol table.  Currently we only get called to
  2351.      read the full symbol table.  -fnf */
  2352.   if (bfd_get_outsymbols (abfd) != NULL)
  2353.     {
  2354.       return (true);
  2355.     }
  2356.  
  2357.   /* Read each raw ELF symbol, converting from external ELF form to
  2358.      internal ELF form, and then using the information to create a
  2359.      canonical bfd symbol table entry.
  2360.  
  2361.      Note that we allocate the initial bfd canonical symbol buffer
  2362.      based on a one-to-one mapping of the ELF symbols to canonical
  2363.      symbols.  We actually use all the ELF symbols, so there will be no
  2364.      space left over at the end.  When we have all the symbols, we
  2365.      build the caller's pointer vector. */
  2366.  
  2367.   if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) == -1)
  2368.     {
  2369.       bfd_error = system_call_error;
  2370.       return (false);
  2371.     }
  2372.  
  2373.   symcount = hdr->sh_size / sizeof (Elf_External_Sym);
  2374.   symbase = (elf_symbol_type *) bfd_zalloc (abfd, symcount * sizeof (elf_symbol_type));
  2375.   sym = symbase;
  2376.  
  2377.   /* Temporarily allocate room for the raw ELF symbols.  */
  2378.   x_symp = (Elf_External_Sym *) bfd_xmalloc (symcount * sizeof (Elf_External_Sym));
  2379.  
  2380.   if (bfd_read ((PTR) x_symp, sizeof (Elf_External_Sym), symcount, abfd) 
  2381.       != symcount * sizeof (Elf_External_Sym))
  2382.     {
  2383.       free ((PTR)x_symp);
  2384.       bfd_error = system_call_error;
  2385.       return (false);
  2386.     }
  2387.   /* Skip first symbol, which is a null dummy.  */
  2388.   for (i = 1; i < symcount; i++)
  2389.     {
  2390.       elf_swap_symbol_in (abfd, x_symp + i, &i_sym);
  2391.       memcpy (&sym->internal_elf_sym, &i_sym, sizeof (Elf_Internal_Sym));
  2392.       memcpy (&sym->native_elf_sym, x_symp + i, sizeof (Elf_External_Sym));
  2393.       sym->symbol.the_bfd = abfd;
  2394.       if (i_sym.st_name > 0)
  2395.     sym->symbol.name = elf_string_from_elf_section(abfd, hdr->sh_link,
  2396.                                i_sym.st_name);
  2397.       else
  2398.     sym->symbol.name = ""; /* perhaps should include the number? */
  2399.  
  2400.       sym->symbol.value = i_sym.st_value;
  2401.       /* FIXME -- this is almost certainly bogus.  It's from Pace
  2402.      Willisson's hasty Solaris support, to pass the sizes of
  2403.      object files or functions down into GDB via the back door, to
  2404.      circumvent some other kludge in how Sun hacked stabs.  --
  2405.      gnu@cygnus.com */
  2406.       /* XXX Size is now stored via a pointer in an elf_symbol_type;
  2407.      should fix gdb then turn this off.  */
  2408.       sym ->symbol.udata = (PTR)i_sym.st_size;
  2409.       /* FIXME -- end of bogosity.  */
  2410.       if (i_sym.st_shndx > 0 && i_sym.st_shndx < SHN_LORESERV)
  2411.     {
  2412.       sym->symbol.section = section_from_elf_index (abfd, i_sym.st_shndx);
  2413.     }
  2414.       else if (i_sym.st_shndx == SHN_ABS)
  2415.     {
  2416.       sym->symbol.section = &bfd_abs_section;
  2417.     }
  2418.       else if (i_sym.st_shndx == SHN_COMMON)
  2419.     {
  2420.       sym->symbol.section = &bfd_com_section;
  2421.     }
  2422.       else if (i_sym.st_shndx == SHN_UNDEF)
  2423.     {
  2424.       sym->symbol.section = &bfd_und_section;
  2425.     }
  2426.       else
  2427.     sym->symbol.section = &bfd_abs_section;
  2428.       
  2429.       switch (ELF_ST_BIND (i_sym.st_info))
  2430.     {
  2431.       case STB_LOCAL:
  2432.         sym->symbol.flags |= BSF_LOCAL;
  2433.         break;
  2434.       case STB_GLOBAL:
  2435.         sym->symbol.flags |= (BSF_GLOBAL | BSF_EXPORT);
  2436.         break;
  2437.       case STB_WEAK:
  2438.         sym->symbol.flags |= BSF_WEAK;
  2439.         break;
  2440.     }
  2441.  
  2442.       switch (ELF_ST_TYPE (i_sym.st_info))
  2443.     {
  2444.       case STT_SECTION:
  2445.         sym->symbol.flags |= BSF_SECTION_SYM | BSF_DEBUGGING;
  2446.         break;
  2447.       case STT_FILE:
  2448.         sym->symbol.flags |= BSF_FILE | BSF_DEBUGGING;
  2449.         break;
  2450.       case STT_FUNC:
  2451.         sym->symbol.flags |= BSF_FUNCTION;
  2452.         break;
  2453.     }
  2454.       /* Is this a definition of $global$?  If so, keep it because it will be
  2455.      needd if any relocations are performed.  */
  2456.       if (!strcmp (sym->symbol.name, "$global$")
  2457.       && sym->symbol.section != &bfd_und_section)
  2458.     {
  2459.       /* @@ Why is this referring to backend data and not a field of
  2460.          abfd?  FIXME */
  2461.       struct elf_backend_data *be_data = (struct elf_backend_data *) abfd->xvec->backend_data;
  2462.  
  2463.       be_data->global_sym = sym;
  2464.     }
  2465.       sym++;
  2466.     }
  2467.  
  2468.   /* We rely on the zalloc to clear out the final symbol entry.  */
  2469.  
  2470.   obj_raw_syms (abfd) = x_symp;
  2471.  
  2472.   bfd_get_symcount(abfd) = symcount = sym - symbase;
  2473.   
  2474.   /* Fill in the user's symbol pointer vector if needed.  */
  2475.   if (symptrs)
  2476.     {
  2477.       sym = symbase;
  2478.       while (symcount-- > 0)
  2479.     {
  2480.       *symptrs++ = &sym->symbol;
  2481.       sym++;
  2482.     }
  2483.       *symptrs = 0;            /* Final null pointer */
  2484.     }
  2485.  
  2486.   return (true);
  2487. }
  2488.  
  2489. /* Return the number of bytes required to hold the symtab vector.
  2490.  
  2491.    Note that we base it on the count plus 1, since we will null terminate
  2492.    the vector allocated based on this size.  However, the ELF symbol table
  2493.    always has a dummy entry as symbol #0, so it ends up even.  */
  2494.  
  2495. unsigned int
  2496. DEFUN (elf_get_symtab_upper_bound, (abfd), bfd *abfd)
  2497. {
  2498.   unsigned int symcount;
  2499.   unsigned int symtab_size = 0;
  2500.   Elf_Internal_Shdr *i_shdrp;
  2501.   Elf_Internal_Shdr *hdr;
  2502.  
  2503.   i_shdrp = elf_elfsections (abfd);
  2504.   if (i_shdrp != NULL)
  2505.     {
  2506.       hdr = i_shdrp + elf_onesymtab (abfd);
  2507.       symcount = hdr->sh_size / sizeof (Elf_External_Sym);
  2508.       symtab_size = (symcount - 1 + 1) * (sizeof (asymbol));
  2509.     }
  2510.   return (symtab_size);
  2511. }
  2512.  
  2513. /*
  2514.     This function return the number of bytes required to store the
  2515.     relocation information associated with section <<sect>>
  2516.     attached to bfd <<abfd>>
  2517.  
  2518. */
  2519. unsigned int
  2520. elf_get_reloc_upper_bound (abfd, asect)
  2521. bfd            *abfd;
  2522. sec_ptr         asect;
  2523. {
  2524.   if (asect->flags & SEC_RELOC)
  2525.     {
  2526.       /* either rel or rela */
  2527.       return asect->_raw_size;
  2528.     }
  2529.   else
  2530.     return (0);
  2531. }
  2532.  
  2533. static boolean
  2534. DEFUN(elf_slurp_reloca_table,(abfd, asect, symbols),
  2535.       bfd            *abfd AND
  2536.       sec_ptr         asect AND
  2537.       asymbol       **symbols)
  2538. {
  2539.   Elf_External_Rela   *native_relocs;
  2540.   arelent        *reloc_cache;
  2541.   arelent        *cache_ptr;
  2542.  
  2543.   unsigned int idx;
  2544.   
  2545.   if (asect->relocation)
  2546.     return true;
  2547.   if (asect->reloc_count == 0)
  2548.     return true;
  2549.   if (asect->flags & SEC_CONSTRUCTOR)
  2550.     return true;
  2551.  
  2552.   bfd_seek (abfd, asect->rel_filepos, SEEK_SET);
  2553.   native_relocs = (Elf_External_Rela *)
  2554.     bfd_alloc(abfd, asect->reloc_count * sizeof(Elf_External_Rela));
  2555.   bfd_read ((PTR) native_relocs,
  2556.         sizeof(Elf_External_Rela), asect->reloc_count, abfd);
  2557.   
  2558.   reloc_cache = (arelent *)
  2559.     bfd_alloc(abfd, (size_t) (asect->reloc_count * sizeof(arelent)));
  2560.  
  2561.   if (! reloc_cache) {
  2562.     bfd_error = no_memory;
  2563.     return false;
  2564.   } 
  2565.   
  2566.   for (idx = 0; idx < asect->reloc_count; idx ++) 
  2567.     {
  2568. #ifdef RELOC_PROCESSING
  2569.       /* sparc, 68k, 88k, 860 use rela only. */
  2570.       /* 386 and we32000 use rel only... fix it for them later. */
  2571.       Elf_Internal_Rela dst;
  2572.       Elf_External_Rela  *src;
  2573.  
  2574.       cache_ptr = reloc_cache + idx;
  2575.       src = native_relocs + idx;
  2576.       elf_swap_reloca_in(abfd, src, &dst);
  2577.  
  2578.       RELOC_PROCESSING(cache_ptr, &dst, symbols, abfd, asect);
  2579. #else
  2580.       Elf_Internal_Rela dst;
  2581.       Elf_External_Rela  *src;
  2582.  
  2583.       cache_ptr = reloc_cache + idx;
  2584.       src = native_relocs + idx;
  2585.  
  2586.       elf_swap_reloca_in(abfd, src, &dst);
  2587.  
  2588.       if(asect->flags & SEC_RELOC)
  2589.     {
  2590.       /* relocatable, so the offset is off of the section */
  2591.       cache_ptr->address = dst.r_offset + asect->vma;
  2592.     }
  2593.       else
  2594.     {
  2595.       /* non-relocatable, so the offset a virtual address */
  2596.       cache_ptr->address = dst.r_offset;
  2597.     }
  2598.       /* ELF_R_SYM(dst.r_info) is the symbol table offset; subtract 1
  2599.      because the first entry is NULL.  */
  2600.       cache_ptr->sym_ptr_ptr = symbols + ELF_R_SYM(dst.r_info) - 1;
  2601.       cache_ptr->addend = dst.r_addend;
  2602.  
  2603.       /* Fill in the cache_ptr->howto field from dst.r_type */
  2604.       {
  2605.     struct elf_backend_data *ebd;
  2606.     ebd = (struct elf_backend_data *) (abfd->xvec->backend_data);
  2607.     (*ebd->elf_info_to_howto)(abfd, cache_ptr, &dst);
  2608.       }
  2609. #endif
  2610.     }
  2611.  
  2612.   asect->relocation = reloc_cache;
  2613.   return true;
  2614. }
  2615.  
  2616.  
  2617. unsigned int
  2618. elf_canonicalize_reloc (abfd, section, relptr, symbols)
  2619. bfd            *abfd;
  2620. sec_ptr         section;
  2621. arelent       **relptr;
  2622. asymbol       **symbols;
  2623. {
  2624.   arelent        *tblptr = section->relocation;
  2625.   unsigned int    count = 0;
  2626.  
  2627.   /* snarfed from coffcode.h */
  2628.   /* FIXME: this could be reloc... */
  2629.   elf_slurp_reloca_table(abfd, section, symbols);
  2630.  
  2631.   tblptr = section->relocation;
  2632.   if (!tblptr)
  2633.     return 0;
  2634.  
  2635.   for (; count++ < section->reloc_count;)
  2636.     *relptr++ = tblptr++;
  2637.  
  2638.   *relptr = 0;
  2639.   return section->reloc_count;
  2640. }
  2641.  
  2642. unsigned int
  2643. DEFUN (elf_get_symtab, (abfd, alocation),
  2644.        bfd            *abfd AND
  2645.        asymbol       **alocation)
  2646. {
  2647.  
  2648.   if (!elf_slurp_symbol_table (abfd, alocation))
  2649.     return (0);
  2650.   else
  2651.     return (bfd_get_symcount (abfd));
  2652. }
  2653.  
  2654. asymbol *
  2655. DEFUN (elf_make_empty_symbol, (abfd),
  2656.        bfd *abfd)
  2657. {
  2658.   elf_symbol_type *newsym;
  2659.  
  2660.   newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
  2661.   if (! newsym)
  2662.     {
  2663.       bfd_error = no_memory;
  2664.       return (NULL);
  2665.     }
  2666.   else
  2667.     {
  2668.       newsym -> symbol.the_bfd = abfd;
  2669.       return (&newsym -> symbol);
  2670.     }
  2671. }
  2672.  
  2673. void 
  2674. DEFUN (elf_print_symbol,(ignore_abfd, filep, symbol, how),
  2675.       bfd            *ignore_abfd AND
  2676.       PTR           filep AND
  2677.       asymbol        *symbol AND
  2678.       bfd_print_symbol_type how)
  2679. {
  2680.   FILE *file = (FILE *)filep;
  2681.   switch (how)
  2682.     {
  2683.     case bfd_print_symbol_name:
  2684.       fprintf(file, "%s", symbol->name);
  2685.       break;
  2686.     case bfd_print_symbol_more:
  2687.       fprintf(file, "elf %lx %lx",
  2688.           symbol->value,
  2689.           symbol->flags);
  2690.       break;
  2691.     case bfd_print_symbol_nm:
  2692.     case bfd_print_symbol_all:
  2693.       {
  2694.     CONST char *section_name;
  2695.     section_name = symbol->section? symbol->section->name : "(*none*)";
  2696.     bfd_print_symbol_vandf((PTR) file, symbol);
  2697.     fprintf(file, " %s\t%s",
  2698.         section_name,
  2699.         symbol->name);
  2700.       }
  2701.       break;
  2702.     }
  2703.  
  2704. }
  2705.  
  2706. alent *
  2707. DEFUN (elf_get_lineno,(ignore_abfd, symbol),
  2708.       bfd            *ignore_abfd AND
  2709.       asymbol        *symbol)
  2710. {
  2711.   fprintf (stderr, "elf_get_lineno unimplemented\n");
  2712.   fflush (stderr);
  2713.   abort ();
  2714.   return (NULL);
  2715. }
  2716.  
  2717. boolean
  2718. DEFUN (elf_set_arch_mach,(abfd, arch, machine),
  2719.       bfd            *abfd AND
  2720.       enum bfd_architecture arch AND
  2721.       unsigned long   machine)
  2722. {
  2723.   /* Allow any architecture to be supported by the elf backend */
  2724.   switch(arch)
  2725.     {
  2726.     case bfd_arch_unknown:    /* EM_NONE */
  2727.     case bfd_arch_sparc:    /* EM_SPARC */
  2728.     case bfd_arch_i386:        /* EM_386 */
  2729.     case bfd_arch_m68k:        /* EM_68K */
  2730.     case bfd_arch_m88k:        /* EM_88K */
  2731.     case bfd_arch_i860:        /* EM_860 */
  2732.     case bfd_arch_mips:        /* EM_MIPS (MIPS R3000) */
  2733.     case bfd_arch_hppa:        /* EM_HPPA (HP PA_RISC) */
  2734.       return  bfd_default_set_arch_mach(abfd, arch, machine);
  2735.     default:
  2736.       return false;
  2737.     }
  2738. }
  2739.  
  2740. boolean
  2741. DEFUN (elf_find_nearest_line,(abfd,
  2742.                   section,
  2743.                   symbols,
  2744.                   offset,
  2745.                   filename_ptr,
  2746.                   functionname_ptr,
  2747.                   line_ptr),
  2748.       bfd            *abfd AND
  2749.       asection       *section AND
  2750.       asymbol       **symbols AND
  2751.       bfd_vma         offset AND
  2752.       CONST char      **filename_ptr AND
  2753.       CONST char       **functionname_ptr AND
  2754.       unsigned int   *line_ptr)
  2755. {
  2756.   return false;
  2757. }
  2758.  
  2759. int 
  2760. DEFUN (elf_sizeof_headers, (abfd, reloc),
  2761.       bfd *abfd AND
  2762.       boolean reloc)
  2763. {
  2764.   fprintf (stderr, "elf_sizeof_headers unimplemented\n");
  2765.   fflush (stderr);
  2766.   abort ();
  2767.   return (0);
  2768. }
  2769.  
  2770. boolean
  2771. DEFUN(elf_set_section_contents, (abfd, section, location, offset, count),
  2772.       bfd *abfd AND
  2773.       sec_ptr section AND
  2774.       PTR location AND
  2775.       file_ptr offset AND
  2776.       bfd_size_type count)
  2777. {
  2778.   int dest_sect;
  2779.   void *contents;
  2780.   if (abfd->output_has_begun == false) /* set by bfd.c handler? */
  2781.     {
  2782.       /* do setup calculations (FIXME) */
  2783.       elf_compute_section_file_positions(abfd);
  2784.       abfd->output_has_begun = true;
  2785.     }
  2786.  
  2787.   dest_sect = elf_section_from_bfd_section(abfd, section);
  2788.   if(!dest_sect)
  2789.     return false;
  2790.  
  2791.   if (bfd_seek (abfd, elf_elfsections(abfd)[dest_sect].sh_offset + offset, SEEK_SET) == -1)
  2792.     return false;
  2793.   if (bfd_write (location, 1, count, abfd) != count)
  2794.     return false;
  2795.   return true;
  2796. }
  2797.  
  2798. void
  2799. DEFUN (elf_no_info_to_howto, (abfd, cache_ptr, dst),
  2800.        bfd *abfd AND
  2801.        arelent *cache_ptr AND
  2802.        Elf_Internal_Rela *dst)
  2803. {
  2804.   abort ();
  2805. }
  2806.